Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class staticVariablesMain {
- public static void main (String [] args) {
- staticVariables member1 = new staticVariables("Drew", "Barrymore");
- staticVariables member2 = new staticVariables("Rachel", "Riley");
- staticVariables member3 = new staticVariables("Rachel", "Atherton");
- staticVariables member4 = new staticVariables("from Hunger Games", "Thingy");
- staticVariables member5 = new staticVariables("Emma", "Stone");
- /*
- * each of these variables have their own two variables but they all share the variable members.
- * Without the static variable, each constructor would need to have their own method.
- */
- }
- }
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- public class staticVariables {
- private String first;
- private String last;
- private static int members = 0;
- /*
- * static keyword means all objects share the same variable
- */
- public staticVariables(String ln, String fn) { // build constructor
- first = fn;
- last = ln;
- members++;
- System.out.printf("%s, %s : My girlfriend #%d\n", first, last, members);
- }
- }
- /*
- * Static variables, when you want all objects to share a single variable value
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement