QUESTIONS LAST TIME: - Refactoring, interfaces, DeathBox spec.. TODAY: - SiteRiter questions - Dave's DeathBoxImpl - Re|new java: static, enum - Grammars and parsing HOMEWORK - Review Java operators, e.g., at http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html &ff or via JPL4 ch 9 through 9.5. Be able to figure what this prints, e.g.: class Test { public static void main(String[] args) { System.out.println(0x2b|~0x2b); } } PROJECT 1 STATUS DAY 2: +--------------+ - Interim deliverable #1 DUE Wed Sep 4, 2013 12NOON MST |UNDER ONE WEEK| +--------------+ ECLIPSE TIP OF THE DAY How to get back to work? QUESTIONS LAST TIME: - Refactoring, interfaces, DeathBox spec.. TODAY: - SiteRiter questions - Dave's DeathBoxImpl - Re|new java: static, enum - Grammars and parsing HOMEWORK - Review Java operators, e.g., at http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html &ff or via JPL4 ch 9 through 9.5. Be able to figure what this prints, e.g.: class Test { public static void main(String[] args) { System.out.println(0x2b|~0x2b); } } PROJECT 1 STATUS DAY 2: +--------------+ - Interim deliverable #1 DUE Wed Sep 4, 2013 12NOON MST |UNDER ONE WEEK| +--------------+ ECLIPSE TIP OF THE DAY How to get back to work? Ctl-Q Last edit location PROJECT 1 QUESTIONS PROJECT 1 TIMELINE ((T.2)) Project timeline ((T.2.1)) When: Wed Aug 28, 2013 What: Project released +-----------------------------------------------------------------------+ |((T.2.2)) INTERIM DELIVERABLE#1 DEADLINE [START+7 DAYS] | |When: Wed Sep 4, 2013 12NOON MST | |What: Last possible ON-TIME code turn-in of: | | Compilable, runnable JUnit tests for the interface | | com.putable.siteriter.SDLParser (but no _actual_ implementation | | is required). Must include _numerous_ tests beyond the examples | | given in the spec. setup() (and/or tests) should presume the | | existence of an com.putable.siteriter.test.SDLParserImpl class | | that implements SDLParser, and make and test instances of | | that class. | +-----------------------------------------------------------------------+ ((T.2.3)) When: Sat Sep 7, 2013 11:59:59AM MST What: Last possible LATE turn-in of INTERIM DELIVERABLE#1 (T.2.2) for any credit must occur BEFORE NOON SAT SEP 7, 2013 PROJECT 1 TIMELINE ((T.2.4)) When: Sat Sep 7, 2013 12NOON MST What: SiteRiter reference implementation made accessible online. +-----------------------------------------------------------------------+ |((T.2.5)) INTERIM DELIVERABLE#2 DEADLINE [START+14 DAYS]| |When: Wed Sep 11, 2013 12OON MST | |What: Laat possible ON-TIME code turn-in of: | | (1) Updated & extended JUnit tests for SDLParser | | (2) A compilable, runnable, clean and feature-complete | | implementation of SDLParserImpl, as | | com.putable.siteriter.YOURNAME.SDLParserImpl, that fully and | | correctly implements com.putable.siteriter.SDLParser. | | All spec-required time and space performance bounds must be | met by your implementation, but your JUnit tests only have | | to test function, not space/time performance. | +-----------------------------------------------------------------------+ ((T.2.6)) When: Sat Sep 14, 2013 12NOON MST What: SDLParser JUnit tests released PROJECT 1 TIMELINE +-----------------------------------------------------------------------+ |((T.2.7)) FINAL DELIVERABLE ON-TIME DEADLINE [START+23 DAYS]| |When: FRI SEP 20, 2013 12NOON MST | |What: Full project latest on-time turn-in: | | (1) A complete, conforming SiteRiterer implementation with test | | suite | | (2) Implementation-specific documentation | +-----------------------------------------------------------------------+ ((T.2.8)) Final deliverable LATEST LATE deadline When: Tue Sep 24, 2013 9AM MST What: Last possible time to turn in Project 1 for any credit. ((T.2.9)) When: Tue Sep 24, 2013 11AM MST What: Project 1 discussion in class REFACTORING HELLO WORLD - THE EXPLODING CLASS What: Provide an abstract base class to encapsulate common behavior Why: An interface provides only method signatures and informal behavioral specs. But often some of the methods that are naturally part of the interface will probably be implemented the same way by (most) all interface implementations. How: (0) Rename the class 'FooImpl' (that implements interface Foo) to something like 'ConcreteFoo'. Then: (1) Move the to-be-shared fields and methods into a new abstract class 'AbstractFoo', that implements interface Foo. (2) Have Concrete Foo extend AbstractFoo rather than implementing Foo AFTER: public interface HelloWorld { public void run() ; } public abstract class AbstractHelloWorld implements HelloWorld { public void run() { System.out.println("Hello World"); } } public class ConcreteHelloWorld extends AbstractHelloWorld { public static void main(String[] args) { HelloWorld hw = new ConcreteHelloWorld(); hw.run(); } } REFACTORING HELLO WORLD - THE FULLY EXPLODED CLASS BEFORE +--------------+ |HelloWorld | |--------------| | | |--------------| |void run() | |void main(..) | +--------------+ REFACTORING HELLO WORLD - THE FULLY EXPLODED CLASS BEFORE AFTER +-----------------------+ |HelloWorld (interface) | +--------------+ |-----------------------| |HelloWorld | | | |--------------| |-----------------------| | | |void run() ; | |--------------| | | |void run() | +-----------------------+ |void main(..) | +--------------+ REFACTORING HELLO WORLD - THE FULLY EXPLODED CLASS BEFORE AFTER +-----------------------+ |HelloWorld (interface) | +--------------+ +-------------------+ |\ |-----------------------| |HelloWorld | |AbstractHelloWorld |...| >| | |--------------| |-------------------| |/ |-----------------------| | | | | |void run() ; | |--------------| |-------------------| | | |void run() | |void run() { .. } | +-----------------------+ |void main(..) | | | +--------------+ +-------------------+ REFACTORING HELLO WORLD - THE FULLY EXPLODED CLASS BEFORE AFTER +-----------------------+ |HelloWorld (interface) | +--------------+ +-------------------+ |\ |-----------------------| |HelloWorld | |AbstractHelloWorld |...| >| | |--------------| |-------------------| |/ |-----------------------| | | | | |void run() ; | |--------------| |-------------------| | | |void run() | |void run() { .. } | +-----------------------+ |void main(..) | | | +--------------+ +-------------------+ ^ / \ --- | +-------------------+ |ConcreteHelloWorld | |-------------------| | | |-------------------| |void main(..) | | | +-------------------+ REFACTORING HELLO WORLD - THE FULLY EXPLODED CLASS BEFORE AFTER +-----------------------+ |HelloWorld (interface) | +--------------+ +-------------------+ |\ |-----------------------| |HelloWorld | |AbstractHelloWorld |...| >| | |--------------| |-------------------| |/ |-----------------------| | | | | |void run() ; | |--------------| |-------------------| | | |void run() | |void run() { .. } | +-----------------------+ |void main(..) | | | +--------------+ +-------------------+ ^ - This is all such a win / \ why, again? --- | +-------------------+ |ConcreteHelloWorld | |-------------------| | | |-------------------| |void main(..) | | | +-------------------+ DEATHBOX OPENED - SPEC ISSUES? ------- Here are the rules for putting things into the DeathBox: 1. If you put anything that's not an integer, or a String containing an integer, into the DeathBox, you die. 2. If you put anything outside 6 to 60 into the DeathBox, you die. 3. If you put anything into the DeathBox that's more than three times the last thing put into the DeathBox, you die. 4. If you put the same thing into the DeathBox more than three times, you die. 5. You can't put anything into the DeathBox after you've died. 6. If the thing you put into the DeathBox doesn't cause you to die according to the rules 1-5, then you live. ------- DEATHBOX OPENED - SPEC ISSUES? ------- Here are the rules for putting things into the DeathBox: +-------+ 1. If you put anything that's not an|integer| or a String containing an integer, into the DeathBox, yo+-------+ 2. If you put anything outside 6 to 60 into the DeathBox, you die. 3. If you put anything into the DeathBox that's more than three times the last thing put into the DeathBox, you die. 4. If you put the same thing into the DeathBox more than three times, you die. 5. You can't put anything into the DeathBox after you've died. 6. If the thing you put into the DeathBox doesn't cause you to die according to the rules 1-5, then you live. ------- It says 'integer', not 'Integer'.. E.g., a short is an integer but not an Integer. Can you put a Short into the DeathBox and live? Dave chose: Yes, ditto Character and Byte, etc -- you can put in anything that's java.lang.Number except Double and Float. DEATHBOX OPENED - SPEC ISSUES? ------- Here are the rules for putting things into the DeathBox: +----------------- 1. ----------+anything that's not an integer, or a|String containing an integer| into the DeathBox, you die. +----------------- ----------+ 2. If you put anything outside 6 to 60 into the DeathBox, you die. 3. If you put anything into the DeathBox that's more than three times the last thing put into the DeathBox, you die. 4. If you put the same thing into the DeathBox more than three times, you die. 5. You can't put anything into the DeathBox after you've died. 6. If the thing you put into the DeathBox doesn't cause you to die according to the rules 1-5, then you live. ------- What does this mean?? Containing _only_ an integer? How represented? "2b|~2b" _contains_ an integer.. So does "twelve", arguably.. "f00ba"?? Dave chose: 'contains' means 'contains only', using Integer.parseInt(String) semantics DEATHBOX OPENED - SPEC ISSUES? ------- Here are the rules for putting things into the DeathBox: 1. If you put anything that's not an integer, or a String containing an integer, into the DeathBox, you die. +---------------+ 2. If you put anything|outside 6 to 60|into the DeathBox, you die. +---------------+ 3. If you put anything into the DeathBox that's more than three times the last thing put into the DeathBox, you die. 4. If you put the same thing into the DeathBox more than three times, you die. 5. You can't put anything into the DeathBox after you've died. 6. If the thing you put into the DeathBox doesn't cause you to die according to the rules 1-5, then you live. ------- 6 and 60 can live? Must be. 5, dead; 6, maybe okay. DEATHBOX OPENED - SPEC ISSUES? ------- Here are the rules for putting things into the DeathBox: 1. If you put anything that's not an integer, or a String containing an integer, into the DeathBox, you die. 2. If you put anything outside 6 to 60 into the DeathBox, you die. 3. If +----+t anything into the DeathBox that's more than three times the|last|thing put into the DeathBox, you die. +----+ 4. If you put the same thing into the DeathBox more than three times, you die. 5. You can't put anything into the DeathBox after you've died. 6. If the thing you put into the DeathBox doesn't cause you to die according to the rules 1-5, then you live. ------- 'Last' meaning 'previous' or 'final'? Is (10, 6, 2) legal? 10 is more than three times the last thing put in (2)... Dave chose: Last means 'previous' DEATHBOX OPENED - SPEC ISSUES? ------- Here are the rules for putting things into the DeathBox: 1. If you put anything that's not an integer, or a String containing an integer, into the DeathBox, you die. 2. If you put anything outside 6 to 60 into the DeathBox, you die. 3. If +----+t anything into the DeathBox that's more than three times the|last|thing put into the DeathBox, you die. +----+ 4. If you put the same thing into the DeathBox more than three times, you die. 5. You can't put anything into the DeathBox after you've died. 6. If the thing you put into the DeathBox doesn't cause you to die according to the rules 1-5, then you live. ------- 'Last' meaning 'previous' or 'final'? Dave chose: Previous.. So what about the first thing? Dave chose: No restriction by rule 3 on first thing DEATHBOX OPENED - SPEC ISSUES? ------- Here are the rules for putting things into the DeathBox: 1. If you put anything that's not an integer, or a String containing an integer, into the DeathBox, you die. 2. If you put anything outside 6 to 60 into the DeathBox, you die. 3. If you put anything into the DeathBox that's more than three times the last thing put into the DeathBox, you die. +----------+ 4. If you put the|same thing|into the DeathBox more than three times, you die. +----------+ 5. You can't put anything into the DeathBox after you've died. 6. If the thing you put into the DeathBox doesn't cause you to die according to the rules 1-5, then you live. ------- Are 'new Integer(3)' and 'new Integer(3)' (or 'new Short(3)') the 'same thing'? They're different objects..(or even different types..) Dave chose: ..Must be same _number_.. ..but it was a close call.. DEATHBOX OPENED - SPEC ISSUES? ------- Here are the rules for putting things into the DeathBox: 1. If you put anything that's not an integer, or a String containing an integer, into the DeathBox, you die. 2. If you put anything outside 6 to 60 into the DeathBox, you die. 3. If you put anything into the DeathBox that's more than three times the last thing put into the DeathBox, you die. 4. If you put the same thing into the DeathBox more than three times, you die. +---------+ 5.|You can't|put anything into the DeathBox after you've died. +---------+ 6. If the thing you put into the DeathBox doesn't cause you to die according to the rules 1-5, then you live. ------- Meaning "it's impossible to" or "I musn't"? If latter, or else what? Dave chose: Or else you die again. DEATHBOX OPENED - SPEC ISSUES? ------- Here are the rules for putting things into the DeathBox: 1. If you put anything that's not an integer, or a String containing an integer, into the DeathBox, you die. 2. If you put anything outside 6 to 60 into the DeathBox, you die. 3. If you put anything into the DeathBox that's more than three times the last thing put into the DeathBox, you die. 4. If you put the same thing into the DeathBox more than three times, you die. 5. You can't put anything into the DeathBox after you've died. 6. If the thing you put into the DeathBox doesn't cause you to die according to the rules 1-5, then you live. ------- Gah! DAVE'S DEATHBOX package com.putable.deathbox; /** * A DeathBox implementation that follows the rules. So Say I. * @author ackley */ public class DeathBoxImpl implements DeathBox { /* (non-Javadoc) * @see com.putable.deathbox.DeathBox#putInto(java.lang.Object) */ public boolean putInto(Object o) { // Rule 5: Check if dead if (dead) return die(); // Rule 1: Normalize out types int num; if (o instanceof String) { try { num = Integer.parseInt((String)o); } catch (NumberFormatException e) { return die(); } } else { if (!(o instanceof Number) || o instanceof Double || DAVE'S DEATHBOX package com.putable.deathbox; /** * A DeathBox implementation that follows the rules. So Say I. * @author ackley */ public class DeathBoxImpl implements DeathBox { .. public boolean putInto(Object o) { // Rule 5: Check if dead if (dead) return die(); // Rule 1: Normalize out types int num; if (o instanceof String) { try { num = Integer.parseInt((String)o); } catch (NumberFormatException e) { return die(); } } else { if (!(o instanceof Number) || o instanceof Double || o instanceof Float) return die(); else num = ((Number) o).intValue(); } .. DAVE'S DEATHBOX .. // Rule 2: Check if outside legal range if (numMAX) return die(); // Rule 3: Check against previous if (last!=null && num>3*last) return die(); // Rule 4: Check if seen too much if (++seen[num] > 3) return die(); // Otherwise remember last, and live last = num; return true; } private boolean die() { dead = true; return false; } private final int MAX=60, MIN=6; /** Holds a reference to the last (normalized input) for comparisons. */ private Integer last = null; /** Tracks our death status */ private boolean dead = false; /** Count how often we've seen things. (Elts 0..MIN-1 are wasted..) */ private int seen[] = new int[MAX+1]; } DEATHBOX RUBRIC - DeathBoxImpl.java (5) Correct by spec where clear, and to your resolutions where not (4) Some small spec break (3) Multiple spec breaks, but largely right (2) Something right (1) Compilation errors (0) Nothing submitted - DeathBoxImplTest.java (5) Complete coverage, including edges, corners, exceptions and errors (4) Good coverage but one or two gaps (3) Fair coverage but significant gaps (2) Only a couple cases tested (1) Compilation errors (0) Nothing submitted - spec-problems.txt (5) Thorough listing of issues with thoughtful resolutions (4) Good identification of issues with plausible resolutions (3) Multiple issues identified, but poor or missing resolutions (2) At least one issue identified (1) Something submitted (0) Nothing submitted RE|NEW JAVA: STATIC/INSTANCE SEQUENCING class InitSeq { static final int STATICFINALFOO = 0; final int FINALFOO = 1; static int STATICFOO = 2; int FIELDFOO = 3; public static void main(String[] args) { int METHODFOO = 4; } } RE|NEW JAVA: STATIC/INSTANCE SEQUENCING: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = val(0); final int FINALFOO = val(1); static int STATICFOO = val(2); int FIELDFOO = val(3); public static void main(String[] args) { int METHODFOO = val(4); } static int val(int num) { System.out.println("step "+num); return num; } } RE|NEW JAVA: STATIC/INSTANCE SEQUENCING: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = val(0); final int FINALFOO = val(1); static int STATICFOO = val(2); int FIELDFOO = val(3); public static void main(String[] args) { int METHODFOO = val(4); } static int val(int num) { System.out.println("step "+num); return num; } } $ javac InitSeq.java;java InitSeq step 0 step 2 step 4 $ RE|NEW JAVA: STATIC/INSTANCE SEQUENCING: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = val(0); final int FINALFOO = val(1); static int STATICFOO = val(2); int FIELDFOO = val(3); public static void main(String[] args) { int METHODFOO = val(4); InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq } static int val(int num) { System.out.println("step "+num); return num; } } RE|NEW JAVA: STATIC/INSTANCE SEQUENCING: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = val(0); final int FINALFOO = val(1); static int STATICFOO = val(2); int FIELDFOO = val(3); public static void main(String[] args) { int METHODFOO = val(4); InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq } static int val(int num) { System.out.println("step "+num); return num; } } $ javac InitSeq.java;java InitSeq step 0 step 2 step 4 step 1 step 3 $ RE|NEW JAVA: STATIC/INSTANCE SEQUENCING: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = val(0); final int FINALFOO = val(1); static int STATICFOO = val(2); int FIELDFOO = val(3); public static void main(String[] args) { int METHODFOO = val(4); InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ } static int val(int num) { System.out.println("step "+num); return num; } } $ javac InitSeq.java;java InitSeq RE|NEW JAVA: STATIC/INSTANCE SEQUENCING: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = val(0); final int FINALFOO = val(1); static int STATICFOO = val(2); int FIELDFOO = val(3); public static void main(String[] args) { int METHODFOO = val(4); InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ } static int val(int num) { System.out.println("step "+num); return num; } } $ javac InitSeq.java;java InitSeq step 0 step 2 step 4 step 1 step 3 step 1 step 3 $ RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = 0; final int FINALFOO = 1; static int STATICFOO = 2; int FIELDFOO = 3; public static void main(String[] args) { int METHODFOO = 4; InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ iseq.STATICFINALFOO = 12; System.out.println(iseq2.STATICFINALFOO); } } RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = 0; final int FINALFOO = 1; static int STATICFOO = 2; int FIELDFOO = 3; public static void main(String[] args) { int METHODFOO = 4; InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ iseq.STATICFINALFOO = 12; System.out.println(iseq2.STATICFINALFOO); } } $ javac InitSeq.java InitSeq.java:10: cannot assign a value to final variable FINALFOO iseq.STATICFINALFOO = 12; 1 error $ RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = 0; final int FINALFOO = 1; static int STATICFOO = 2; int FIELDFOO = 3; public static void main(String[] args) { int METHODFOO = 4; InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ iseq.STATICFOO = 12; System.out.println(iseq2.STATICFOO); } } RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = 0; final int FINALFOO = 1; static int STATICFOO = 2; int FIELDFOO = 3; public static void main(String[] args) { int METHODFOO = 4; InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ iseq.STATICFOO = 12; System.out.println(iseq2.STATICFOO); } } $ javac InitSeq.java;java InitSeq 12 $ RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = 0; final int FINALFOO = 1; static int STATICFOO = 2; int FIELDFOO = 3; public static void main(String[] args) { int METHODFOO = 4; InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ iseq.STATICFOO = 12; System.out.println(iseq2.STATICFOO); } } $ javac InitSeq.java;java InitSeq 12 $ 'static' fields are CLASS-level; they are SHARED by all instances. --> Normally you DON'T write 'instance.staticfield' like I did.. But it is legal.. RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = 0; final int FINALFOO = 1; static int STATICFOO = 2; int FIELDFOO = 3; public static void main(String[] args) { int METHODFOO = 4; InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ InitSeq.STATICFOO = 12; System.out.println(InitSeq.STATICFOO); // Now it's no surprise.. } } $ javac InitSeq.java;java InitSeq 12 $ 'static' fields are CLASS-level; they are SHARED by all instances. --> Typically you write 'ClassName.staticfield', like that.. 'InitSeq.STATICFOO' RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = new FooBar(); final int FINALFOO = 1; static int staticFoo = 2; // Non-final fields should be int fieldFoo = 3; // initial lowercase, mixed case public static void main(String[] args) { int METHODFOO = 4; InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ staticFoo = 12; System.out.println(staticFoo); // _inside_ the class, no need to say } } $ javac InitSeq.java;java InitSeq 12 $ 'static' fields are CLASS-level; they are SHARED by all instances. --> Typically you write 'ClassName.staticfield', except within the class itself, you just say 'staticfield' or 'field'.. RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATICFINALFOO = 0; final int FINALFOO = 1; static int staticFoo = 2; // Non-final fields should be int fieldFoo = 3; // initial lowercase, mixed case public static void main(String[] args) { int methodFoo = 4; // Same for method variables InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ staticFoo = 12; System.out.println(staticFoo); // _inside_ the class, no need to say } } $ javac InitSeq.java;java InitSeq 12 $ 'static' fields are CLASS-level; they are SHARED by all instances. --> Typically you write 'ClassName.staticfield', except within the class itself, you just say 'staticfield' or 'field'.. RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATIC_FINAL_FOO = 0; // Final fields are all caps final int FINAL_FOO = 1; // with '_' between words static int staticFoo = 2; // Non-final fields should be int fieldFoo = 3; // initial lowercase, mixed case public static void main(String[] args) { int methodFoo = 4; // Same for method variables InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ staticFoo = 12; System.out.println(staticFoo); // _inside_ the class, no need to say } } $ javac InitSeq.java;java InitSeq 12 $ 'static' fields are CLASS-level; they are SHARED by all instances. --> Typically you write 'ClassName.staticfield', except within the class itself, you just say 'staticfield' or 'field'.. RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATIC_FINAL_FOO = 0; // Final fields are all caps final int FINAL_FOO = 1; // with '_' between words static int staticFoo = 2; // Non-final fields should be int fieldFoo = 3; // initial lowercase, mixed case public static void main(String[] args) { int methodFoo = 4; // Same for method variables InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ fieldFoo = 12; System.out.println(fieldFoo); } } RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATIC_FINAL_FOO = 0; // Final fields are all caps final int FINAL_FOO = 1; // with '_' between words static int staticFoo = 2; // Non-final fields should be int fieldFoo = 3; // initial lowercase, mixed case public static void main(String[] args) { int methodFoo = 4; // Same for method variables InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ fieldFoo = 12; System.out.println(fieldFoo); } } $ javac InitSeq.java;java InitSeq InitSeq.java:10: non-static variable fieldFoo cannot be referenced from fieldFoo = 12; a static context ^ InitSeq.java:11: non-static variable fieldFoo cannot be referenced from System.out.println(fieldFoo); a static context ^ 2 errors RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATIC_FINAL_FOO = 0; // Final fields are all caps final int FINAL_FOO = 1; // with '_' between words static int staticFoo = 2; // Non-final fields should be int fieldFoo = 3; // initial lowercase, mixed case public static void main(String[] args) { int methodFoo = 4; // Same for method variables InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ iseq.fieldFoo = 12; System.out.println(iseq2.fieldFoo); } } RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATIC_FINAL_FOO = 0; // Final fields are all caps final int FINAL_FOO = 1; // with '_' between words static int staticFoo = 2; // Non-final fields should be int fieldFoo = 3; // initial lowercase, mixed case public static void main(String[] args) { int methodFoo = 4; // Same for method variables InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ iseq.fieldFoo = 12; System.out.println(iseq2.fieldFoo); } } $ javac InitSeq.java;java InitSeq 3 $ RE|NEW JAVA: STATIC/INSTANCE STATE: WHAT DOES THIS PROGRAM PRINT? class InitSeq { static final int STATIC_FINAL_FOO = 0; // Final fields are all caps final int FINAL_FOO = 1; // with '_' between words static int staticFoo = 2; // Non-final fields should be int fieldFoo = 3; // initial lowercase, mixed case public static void main(String[] args) { int methodFoo = 4; // Same for method variables InitSeq iseq = new InitSeq(); // Now build an _instance_ of InitSeq InitSeq iseq2 = new InitSeq(); // Build another _instance_ iseq.fieldFoo = 12; System.out.println(iseq2.fieldFoo); } } $ javac InitSeq.java;java InitSeq 3 $ -> NON-static fields are NOT shared among instances; each instance has ITS OWN SEPARATE set. -> You have to have an instance around to refer to a non-static field. A static method can't refer to non-static fields without an instance. RE|NEW JAVA: STATIC/INSTANCE STATE: UPSHOTS - 'static' stuff is associated with the class itself = static fields 'live in the class' and (thus) are shared by instances = static methods can be used without having access to an instance RE|NEW JAVA: STATIC/INSTANCE STATE: UPSHOTS - 'static' stuff is associated with the class itself = static fields 'live in the class' and (thus) are shared by instances = static methods can be used without having access to an instance - non-static stuff is associated with instances of a class = non-static fields 'live in the instance' = non-static methods can only be called when an instance is accessible RE|NEW JAVA: STATIC/INSTANCE STATE: UPSHOTS - 'static' stuff is associated with the class itself = static fields 'live in the class' and (thus) are shared by instances = static methods can be used without having access to an instance - non-static stuff is associated with instances of a class = non-static fields 'live in the instance' = non-static methods can only be called when an instance is accessible - static, class-level stuff is initialized when a class is loaded (actually, really just before it's needed, I think..) RE|NEW JAVA: STATIC/INSTANCE STATE: UPSHOTS - 'static' stuff is associated with the class itself = static fields 'live in the class' and (thus) are shared by instances = static methods can be used without having access to an instance - non-static stuff is associated with instances of a class = non-static fields 'live in the instance' = non-static methods can only be called when an instance is accessible - static, class-level stuff is initialized when a class is loaded (actually, really just before it's needed, I think..) - object instances are created using 'new'. (There's one other way, but..) = non-static fields are initialized during the creation of an instance RE|NEW JAVA: STATIC/INSTANCE STATE: UPSHOTS - 'static' stuff is associated with the class itself = static fields 'live in the class' and (thus) are shared by instances = static methods can be used without having access to an instance - non-static stuff is associated with instances of a class = non-static fields 'live in the instance' = non-static methods can only be called when an instance is accessible - static, class-level stuff is initialized when a class is loaded (actually, really just before it's needed, I think..) - object instances are created using 'new'. (There's one other way, but..) = non-static fields are initialized during the creation of an instance - Capitalization counts = ClassNames are leading capital, mixed caps = methodNames and non-final fieldNames are leading lowercase, mixed caps = final FIELD_NAMES are all caps, _ word-separated