Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class Student { private int studentId; private String name; private static String college = "Oswego"; public Student (int studentId, String name) { this.studentId = studentId; this.name = name; } public static String getCollege(){ return college; } public static void setCollege(String c){ college = c; } } |
StaticTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class StaticTest { public static void main(String[] args){ Student s1 = new Student(800, "Jim"); Student s2 = new Student(801, "Jane"); System.out.println(Student.getCollege()); Student.setCollege("Oswego State"); System.out.println(Student.getCollege()); // If the variable or method is owned by the class // (i.e., is static), access it through the class! /* System.out.println(s1.getCollege()); System.out.println(s2.getCollege()); */ } } |
Box.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class Box { private int sides; private String color; public Box(String color, int sides){ this.color = color; this.sides = sides; } // Create a Box with 6 sides by default. public Box(String color){ // What we wrote in class: //this.color = color; //this.sides = 6; // A better way to do it! this(color, 6); } } |
ImmutableStrings.java
Notes Version
1 2 3 4 5 6 7 8 9 10 |
public class ImmutableStrings { public static void main(String[] args) { String one = "someString"; String two = "someString"; System.out.println(one.equals(two)); System.out.println(one == two); } } |
Comparison Version
1 2 3 4 5 6 7 8 9 10 |
public class ImmutableStrings { public static void main(String[] args) { String one = new String("someString"); String two = new String("someString"); System.out.println(one.equals(two)); System.out.println(one == two); } } |