Advertisement
Guest User

Untitled

a guest
Apr 8th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1.  
  2. public class staticVariablesMain {
  3.  
  4.     public static void main (String [] args) {
  5.        
  6.         staticVariables member1 = new staticVariables("Drew", "Barrymore");
  7.         staticVariables member2 = new staticVariables("Rachel", "Riley");
  8.         staticVariables member3 = new staticVariables("Rachel", "Atherton");
  9.         staticVariables member4 = new staticVariables("from Hunger Games", "Thingy");
  10.         staticVariables member5 = new staticVariables("Emma", "Stone");
  11.        
  12.         /*
  13.          * each of these variables have their own two variables but they all share the variable members.
  14.          * Without the static variable, each constructor would need to have their own method.
  15.          */
  16.     }
  17.    
  18. }
  19.  
  20.  
  21. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  22.  
  23. public class staticVariables {
  24.  
  25.     private String first;
  26.     private String last;
  27.     private static int members = 0;
  28.  
  29.     /*
  30.      * static keyword means all objects share the same variable
  31.      */
  32.  
  33.     public staticVariables(String ln, String fn) { // build constructor
  34.         first = fn;
  35.         last = ln;
  36.         members++;
  37.  
  38.         System.out.printf("%s, %s : My girlfriend #%d\n", first, last, members);
  39.     }
  40.  
  41.    
  42.    
  43. }
  44.  
  45. /*
  46.  * Static variables, when you want all objects to share a single variable value
  47.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement