Static and Modeling
> Click or hit Control-Enter to run Example.main above
Sorry, Cubbies

Review: The static
Keyword
public class Course {
public static int count = 0;
public static void printName() {
System.out.println("Name");
}
}
public class Example {
public static void main(String[] unused) {
// We can call printName without creating an instance
Course.printName();
// We can increment count without creating an instance
Course.count++;
}
}
static
methods and variables belong to the class, not to a specific
instance.
Review: static
Methods
public class Course {
public static void printName() {
System.out.println("Name");
}
}
public class Example {
public static void main(String[] unused) {
// This works
Course.printName();
// This also works
Course CS125 = new Course();
CS125.printName();
}
}
-
static
methods are called directly on the class, rather than on an instance -
…but they can be called on the instance as well.
Review: static
Methods and this
public class Course {
public String name;
public static void printName() {
// This doesn't work
System.out.println(this.name);
}
}
static
methods can be called without an instance, and so can’t use this
Review: static
Variables
public class Course {
public static int count = 0;
public void printCount() {
System.out.println(count);
}
}
public class Example {
public static void main(String[] unused) {
Course CS125 = new Course();
Course CS225 = new Course();
Course.count++;
CS125.printCount();
CS225.printCount();
}
}
static
variables are shared by all instances of a given class
Review: static
and public
public
and private
also work on static variables and methods
-
public
: the (static) variable can be read or written by anyone -
private
: the (static) variable can only read or written by methods defined on that class -
public
: the (static) method can be called by anyone -
private
: the (static) method can only be called by other methods on that class
static
Object Creation Method
We can also use a class method to be able to return null
when creating a new
object if invalid parameters are supplied.
> Click or hit Control-Enter to run Example.main above
final
and Constants
class Example {
/** The number of hours of sleep you should get per night. */
public static final int HOURS_PER_NIGHT = 8;
...
}
In Java a final
variable cannot be modified.
You usually see this done to establish useful constant values—which can be
either public
or private
> Click or hit Control-Enter to run Example.main above
Questions About static
or final
?
> Click or hit Control-Enter to run Example.main above
Inheritance
public class Pet {
protected String name;
protected String type;
public void printMe() {
System.out.println("I'm a " + this.type + " named " + this.name);
}
}
public class Dog extends Pet {
Dog(String name) {
this.name = name;
this.type = "Dog";
}
}
Dog chuchu = new Dog("Chuchu");
chuchu.printMe();
Java allows objects to inherit state and behavior from another class.
Inheritance Terminology
public class Pet { }
public class Dog extends Pet { }
public class Cat extends Pet { }
In Java we establish inheritance using the extends
keyword.
-
Dog
extendsPet
and so inherits state and behavior fromPet
-
Cat
also extendsPet
and so also inherits state and behavior fromPet
-
We sometimes call
Pet
Dog
's andCat
's parent class -
We sometimes call
Dog
andCat
Pet
's children
More Inheritance Terminology
public class Pet { }
public class Dog extends Pet { }
public class Mutt extends Dog { }
In Java we can have have multiple levels of inheritance.
-
Dog
extendsPet
and so inherits state and behavior fromPet
-
Mutt
extendsDog
and so inherits state and behavior fromDog
andPet
-
We sometimes call
Pet
andDog
Mutt
's ancestors -
We sometimes call
Dog
andMutt
Pet
's descendants
protected
public class Pet {
public String name; // Anyone can set me
private String secret; // Only I can set this value
protected String type; // My descendants can use this value
}
public class Dog extends Pet {
Dog(String name) {
this.name = name;
this.type = "Dog";
}
}
-
public
: the variable can be read or written by anyone -
private
: the variable can only read or written by methods defined on that class -
protected
: the variable can only read or written by methods defined on that class or its descendants
> Click or hit Control-Enter to run Example.main above
The Dirty Truth About protected
public class Pet {
protected String name;
}
public class Dog extends Pet {
Dog(String name) {
this.name = name;
}
}
public class Example {
public static void main(String[] unused) {
Dog chuchu = new Dog("Chuchu");
chuchu.name = "Xyz"; // This works...
}
}
-
protected
: the variable can read or written by methods defined on that class or its descendants… in any package -
protected
: the variable can also be read and written by any method in the same package
public
, private
, and protected
Variables:
-
public
: the variable can be read or written by anyone -
private
: the variable can only read or written by methods defined on that class -
protected
: the variable can be read or written by methods defined on any descendant of that class in any package or any class in the same package
Methods:
-
public
: the method can be called by anyone -
private
: the method can only be called by other methods on that class -
protected
: the method can be called by other methods defined on any descendant of that class in any package or any class in the same package
super
Constructor
public class Pet {
protected String type;
Pet(String setType) {
this.type = setType;
}
}
public class Dog extends Pet {
private String breed;
Dog(String setBreed) {
super("Dog");
this.breed = setBreed;
}
}
Java classes can access their parent’s constructor using the super
keyword.
This must be the first thing done in a child constructor.
> Click or hit Control-Enter to run Example.main above
Announcements
-
Good luck on MP3! Get started.