Objects are created with the keyword new ★ Stay in memory for the life of the Object ★ Unique to each instance of an Object ★ This weeks Sunday Funnies don’t have the same content as previous Sunday Funnies “Sunday Funnies” are the comics in the Sunday newspapers.
comic? ★ Frames, Color (or lack thereof), Speech Bubbles, Author, etc. ★ Each of these would be an instance variable. ★ We can also create instance methods to modify the instance variables. ★ colorFrame, addPhrase From http://xkcd.com/452 From http://frustratedbunny.com/2011/03/22/frustrated-bunny-6-dad-dad
Instance variables private String author; // author of this comic private int frames; // number of frames in comic private String[] speechBubbles; // what the cartoons say } ★ Let’s specify a constructor! ★ The constructor will instantiate all of our instance variables. ★ Let’s assume 1 speechBubble per frame.
to creating a object are identifying the common features among the object being created (dogs have legs, tails, a breed, gender, etc.) ★ Then identify the data type each feature or attribute should be (don’t forget, we can always make our own later) ★ Then create meaningful methods for manipulating or accessing the data.
Instance variables ... /* * Sets the phrase for a given frame. */ public void addPhrase(final int frameNumber, final String nPhrase) { // Frame number starts at 1, but Java arrays start at 0 speechBubbles[frameNumber - 1] = nPhrase; } /* * Returns the current phrase for the given frame */ public String getPhrase(final int frameNumber) { if (speechBubbles[frameNumber - 1] != null) { return speechBubbles[frameNumber - 1]; } return ""; } }
to know how many comics are in existence. How would we do this? ★ Static Variables! ★ Let’s say we wanted to give each comic an ID. How would we do this? ★ Instance Variables! ★ So let’s do it!
Instance variables private String author; // author of this comic private int frames; // number of frames in comic private String[] speechBubbles; // what the cartoons say // Static Variables public static int numberOfComics = 0; // Number of comics in existence public Comic(final String nAuthor, final int nFrames) { // Increate the static count of the number of comics numberOfComics++; // Data store in this instance author = nAuthor; frames = nFrames; speechBubbles = new String[frames]; } ... }
Instance variables private String author; // author of this comic private int frames; // number of frames in comic private String[] speechBubbles; // what the cartoons say // Static Variables public static int numberOfComics = 0; // Number of comics in existence public Comic(final String nAuthor, final int nFrames) { // Increate the static count of the number of comics numberOfComics++; // Data store in this instance author = nAuthor; frames = nFrames; speechBubbles = new String[frames]; } ... } Static
Instance variables private String author; // author of this comic private int frames; // number of frames in comic private String[] speechBubbles; // what the cartoons say // Static Variables public static int numberOfComics = 0; // Number of comics in existence public Comic(final String nAuthor, final int nFrames) { // Increate the static count of the number of comics numberOfComics++; // Data store in this instance author = nAuthor; frames = nFrames; speechBubbles = new String[frames]; } ... } Not Final, because we modify its reference
Instance variables private String author; // author of this comic private int frames; // number of frames in comic private String[] speechBubbles; // what the cartoons say // Static Variables public static int numberOfComics = 0; // Number of comics in existence public Comic(final String nAuthor, final int nFrames) { // Increate the static count of the number of comics numberOfComics++; // Data store in this instance author = nAuthor; frames = nFrames; speechBubbles = new String[frames]; } ... } Keeps track of number of comics
// author of this comic private int frames; // number of frames in comic private String[] speechBubbles; // what the cartoons say private int comicID; // the ID of the comic // Static Variables public static int numberOfComics = 0; // Number of comics in existence public Comic(final String nAuthor, final int nFrames) { // Increate the static count of the number of comics numberOfComics++; // Data store in this instance author = nAuthor; frames = nFrames; speechBubbles = new String[frames]; // Set the comic ID comicID = numberOfComics; } } Comic ID
// author of this comic private int frames; // number of frames in comic private String[] speechBubbles; // what the cartoons say private int comicID; // the ID of the comic // Static Variables public static int numberOfComics = 0; // Number of comics in existence public Comic(final String nAuthor, final int nFrames) { // Increate the static count of the number of comics numberOfComics++; // Data store in this instance author = nAuthor; frames = nFrames; speechBubbles = new String[frames]; // Set the comic ID comicID = numberOfComics; } } Comic ID New Instance Variable
// author of this comic private int frames; // number of frames in comic private String[] speechBubbles; // what the cartoons say private int comicID; // the ID of the comic // Static Variables public static int numberOfComics = 0; // Number of comics in existence public Comic(final String nAuthor, final int nFrames) { // Increate the static count of the number of comics numberOfComics++; // Data store in this instance author = nAuthor; frames = nFrames; speechBubbles = new String[frames]; // Set the comic ID comicID = numberOfComics; } } Comic ID Where the magic happens.
values for instance variables ★ Pretty nifty, eh? ★ Ok, this is a lame example, but think of the possibilities. You can have a static ArrayList (or HashMap or anything) that stores data to share with all of your objects.
// author of this comic private int frames; // number of frames in comic private String[] speechBubbles; // what the cartoons say private int comicID; // the ID of the comic // Static Variables public static int numberOfComics = 0; // Number of comics in existence public Comic(final String nAuthor, final int nFrames) { // Increate the static count of the number of comics numberOfComics++; // Data store in this instance author = nAuthor; frames = nFrames; speechBubbles = new String[frames]; // Set the comic ID comicID = numberOfComics; } /* * Sets the phrase for a given frame. */ public void addPhrase(final int frameNumber, final String nPhrase) { // Frame number starts at 1, but Java arrays start at 0 speechBubbles[frameNumber - 1] = nPhrase; } /* * Returns the current phrase for the given frame */ public String getPhrase(final int frameNumber) { if (speechBubbles[frameNumber - 1] != null) { return speechBubbles[frameNumber - 1]; } return ""; } /* * Returns the ID of the comic */ public int getID() { return comicID; } /* * Returns the ID of the comic followed by the content * of the frames */ public String toString() { StringBuilder toStr = new StringBuilder(); toStr.append(comicID + ": "); for (int frame = 1; frame <= frames; frame++) { toStr.append(this.getPhrase(frame)); } return toStr.toString(); } } Comic Class
main(final String[] args) { // Chicken Joke... Comic chicken = new Comic("Amy", 2); chicken.addPhrase(1, "Why did the chicken cross the road?"); chicken.addPhrase(2, "No one knows, it didn't make it to the other side..."); // Bar Jokes... Comic bar[] = new Comic[2]; bar[0] = new Comic("Hanah", 2); bar[0].addPhrase(1, "3 men walk into a bar."); bar[0].addPhrase(2, "The fourth one ducks."); bar[1] = new Comic("Miles", 2); bar[1].addPhrase(1, "A Horse walks into a bar."); bar[1].addPhrase(2, "The bartender says, 'Holy Crap, There's a horse in my bar!'"); // Print out ID's... System.out.printf ("There are %d comics.%n", Comic.numberOfComics); System.out.printf ("Chicken Joke has ID of %d%n", chicken.getID()); System.out.printf ("Bar[0] Joke has ID of %d%n", bar[0].getID()); System.out.printf ("Bar[1] Joke has ID of %d%n", bar[1].getID()); // Random bar joke System.out.println(); System.out.println(bar[((int)(Math.random() + 0.5))]); } }
main(final String[] args) { // Chicken Joke... Comic chicken = new Comic("Amy", 2); chicken.addPhrase(1, "Why did the chicken cross the road?"); chicken.addPhrase(2, "No one knows, it didn't make it to the other side..."); // Bar Jokes... Comic bar[] = new Comic[2]; bar[0] = new Comic("Hanah", 2); bar[0].addPhrase(1, "3 men walk into a bar."); bar[0].addPhrase(2, "The fourth one ducks."); bar[1] = new Comic("Miles", 2); bar[1].addPhrase(1, "A Horse walks into a bar."); bar[1].addPhrase(2, "The bartender says, 'Holy Crap, There's a horse in my bar!'"); // Print out ID's... System.out.printf ("There are %d comics.%n", Comic.numberOfComics); System.out.printf ("Chicken Joke has ID of %d%n", chicken.getID()); System.out.printf ("Bar[0] Joke has ID of %d%n", bar[0].getID()); System.out.printf ("Bar[1] Joke has ID of %d%n", bar[1].getID()); // Random bar joke System.out.println(); System.out.println(bar[((int)(Math.random() + 0.5))]); } } Create instances of comics.
main(final String[] args) { // Chicken Joke... Comic chicken = new Comic("Amy", 2); chicken.addPhrase(1, "Why did the chicken cross the road?"); chicken.addPhrase(2, "No one knows, it didn't make it to the other side..."); // Bar Jokes... Comic bar[] = new Comic[2]; bar[0] = new Comic("Hanah", 2); bar[0].addPhrase(1, "3 men walk into a bar."); bar[0].addPhrase(2, "The fourth one ducks."); bar[1] = new Comic("Miles", 2); bar[1].addPhrase(1, "A Horse walks into a bar."); bar[1].addPhrase(2, "The bartender says, 'Holy Crap, There's a horse in my bar!'"); // Print out ID's... System.out.printf ("There are %d comics.%n", Comic.numberOfComics); System.out.printf ("Chicken Joke has ID of %d%n", chicken.getID()); System.out.printf ("Bar[0] Joke has ID of %d%n", bar[0].getID()); System.out.printf ("Bar[1] Joke has ID of %d%n", bar[1].getID()); // Random bar joke System.out.println(); System.out.println(bar[((int)(Math.random() + 0.5))]); } } Access instances.
main(final String[] args) { // Chicken Joke... Comic chicken = new Comic("Amy", 2); chicken.addPhrase(1, "Why did the chicken cross the road?"); chicken.addPhrase(2, "No one knows, it didn't make it to the other side..."); // Bar Jokes... Comic bar[] = new Comic[2]; bar[0] = new Comic("Hanah", 2); bar[0].addPhrase(1, "3 men walk into a bar."); bar[0].addPhrase(2, "The fourth one ducks."); bar[1] = new Comic("Miles", 2); bar[1].addPhrase(1, "A Horse walks into a bar."); bar[1].addPhrase(2, "The bartender says, 'Holy Crap, There's a horse in my bar!'"); // Print out ID's... System.out.printf ("There are %d comics.%n", Comic.numberOfComics); System.out.printf ("Chicken Joke has ID of %d%n", chicken.getID()); System.out.printf ("Bar[0] Joke has ID of %d%n", bar[0].getID()); System.out.printf ("Bar[1] Joke has ID of %d%n", bar[1].getID()); // Random bar joke System.out.println(); System.out.println(bar[((int)(Math.random() + 0.5))]); } } Notice we use the name of an object, like bar[0] or chicken
main(final String[] args) { // Chicken Joke... Comic chicken = new Comic("Amy", 2); chicken.addPhrase(1, "Why did the chicken cross the road?"); chicken.addPhrase(2, "No one knows, it didn't make it to the other side..."); // Bar Jokes... Comic bar[] = new Comic[2]; bar[0] = new Comic("Hanah", 2); bar[0].addPhrase(1, "3 men walk into a bar."); bar[0].addPhrase(2, "The fourth one ducks."); bar[1] = new Comic("Miles", 2); bar[1].addPhrase(1, "A Horse walks into a bar."); bar[1].addPhrase(2, "The bartender says, 'Holy Crap, There's a horse in my bar!'"); // Print out ID's... System.out.printf ("There are %d comics.%n", Comic.numberOfComics); System.out.printf ("Chicken Joke has ID of %d%n", chicken.getID()); System.out.printf ("Bar[0] Joke has ID of %d%n", bar[0].getID()); System.out.printf ("Bar[1] Joke has ID of %d%n", bar[1].getID()); // Random bar joke System.out.println(); System.out.println(bar[((int)(Math.random() + 0.5))]); } } Access static variable.
main(final String[] args) { // Chicken Joke... Comic chicken = new Comic("Amy", 2); chicken.addPhrase(1, "Why did the chicken cross the road?"); chicken.addPhrase(2, "No one knows, it didn't make it to the other side..."); // Bar Jokes... Comic bar[] = new Comic[2]; bar[0] = new Comic("Hanah", 2); bar[0].addPhrase(1, "3 men walk into a bar."); bar[0].addPhrase(2, "The fourth one ducks."); bar[1] = new Comic("Miles", 2); bar[1].addPhrase(1, "A Horse walks into a bar."); bar[1].addPhrase(2, "The bartender says, 'Holy Crap, There's a horse in my bar!'"); // Print out ID's... System.out.printf ("There are %d comics.%n", Comic.numberOfComics); System.out.printf ("Chicken Joke has ID of %d%n", chicken.getID()); System.out.printf ("Bar[0] Joke has ID of %d%n", bar[0].getID()); System.out.printf ("Bar[1] Joke has ID of %d%n", bar[1].getID()); // Random bar joke System.out.println(); System.out.println(bar[((int)(Math.random() + 0.5))]); } } Notice we use the name of the Class.