Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java Instance and Static Variables

Java Instance and Static Variables

A basic introduction to Instance and Static Variables.

Avatar for Miles Wallio

Miles Wallio

October 30, 2011
Tweet

Other Decks in Programming

Transcript

  1. Instance Methods and Variables ★ Tied to an Object ★

    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.
  2. Let’s Create A Comic Object... ★ What’s common to each

    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
  3. Let’s Create A Comic Object... public class Comic { //

    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.
  4. Let’s Create A Comic Object... public class Comic { //

    Instance variables ... public Comic(final String nAuthor, final int nFrames) { author = nAuthor; frames = nFrames; speechBubbles = new String[frames]; } }
  5. Let’s Create A Comic Object... ★ So the first steps

    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.
  6. Let’s Create A Comic Object... public class Comic { //

    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 ""; } }
  7. Cool Beans ★ Now we have a basic comic object

    that we can use ★ Useful? Not Really. Good Example? Hopefully. ★ Notice nothing was static
  8. Static Variables and Methods ★ A Single Copy exists ★

    Tied to the Class itself, not tied to Objects ★ Accessible at any time ★ Useful for storing Constants ★ Useful for sharing data among Objects
  9. Let’s Edit a Comic Object... ★ Let’s say we wanted

    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!
  10. This has nothing to do with static variables Even though

    it has nothing to do with static variables, it has the word static... Image from http://xkcd.com/649.
  11. Number of Comics in Existence public class Comic { //

    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]; } ... }
  12. Number of Comics in Existence public class Comic { //

    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
  13. Number of Comics in Existence public class Comic { //

    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
  14. Number of Comics in Existence public class Comic { //

    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
  15. What about Comic ID? ★ If we’re giving the comic

    an ID based off the number of the comic (i.e. first created is 1, second is 2, ...), then ID is equal to the current number of comics
  16. public class Comic { // Instance variables private String author;

    // 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
  17. public class Comic { // Instance variables private String author;

    // 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
  18. public class Comic { // Instance variables private String author;

    // 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.
  19. Comic ID ★ We can use static variables to determine

    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.
  20. public class Comic { // Instance variables private String author;

    // 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
  21. An Example Program public class ComicCollection { public static void

    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))]); } }
  22. An Example Program public class ComicCollection { public static void

    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.
  23. An Example Program public class ComicCollection { public static void

    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.
  24. An Example Program public class ComicCollection { public static void

    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
  25. An Example Program public class ComicCollection { public static void

    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.
  26. An Example Program public class ComicCollection { public static void

    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.
  27. Output There are 3 comics. Chicken Joke has ID of

    1 Bar[0] Joke has ID of 2 Bar[1] Joke has ID of 3 2: 3 men walk into a bar. The fourth one ducks.
  28. Output There are 3 comics. Chicken Joke has ID of

    1 Bar[0] Joke has ID of 2 Bar[1] Joke has ID of 3 2: 3 men walk into a bar. The fourth one ducks. Statically stored data.
  29. Output There are 3 comics. Chicken Joke has ID of

    1 Bar[0] Joke has ID of 2 Bar[1] Joke has ID of 3 2: 3 men walk into a bar. The fourth one ducks. Chicken Comic ID
  30. Output There are 3 comics. Chicken Joke has ID of

    1 Bar[0] Joke has ID of 2 Bar[1] Joke has ID of 3 2: 3 men walk into a bar. The fourth one ducks. Bar[0] Joke Comic ID
  31. Output There are 3 comics. Chicken Joke has ID of

    1 Bar[0] Joke has ID of 2 Bar[1] Joke has ID of 3 2: 3 men walk into a bar. The fourth one ducks. Bar[1] Joke Comic ID
  32. Output There are 3 comics. Chicken Joke has ID of

    1 Bar[0] Joke has ID of 2 Bar[1] Joke has ID of 3 2: 3 men walk into a bar. The fourth one ducks. Comic ID
  33. Output There are 3 comics. Chicken Joke has ID of

    1 Bar[0] Joke has ID of 2 Bar[1] Joke has ID of 3 2: 3 men walk into a bar. The fourth one ducks. Speech Bubble 1
  34. Output There are 3 comics. Chicken Joke has ID of

    1 Bar[0] Joke has ID of 2 Bar[1] Joke has ID of 3 2: 3 men walk into a bar. The fourth one ducks. Speech Bubble 2
  35. Output There are 3 comics. Chicken Joke has ID of

    1 Bar[0] Joke has ID of 2 Bar[1] Joke has ID of 3 2: 3 men walk into a bar. The fourth one ducks. This is Bar[0]’s Joke