Visibility and Static
> Click or hit Control-Enter to run Example.main above
Access Modifiers
public class Person {
public String name;
private int age;
private void printName() {
System.out.println(this.name);
}
public int getAge() {
return this.age;
}
}
Java provides ways to protect instance variables and methods. We refer to these as access modifiers:
Variable Access Modifiers
public class Person {
// Anybody can modify name
public String name;
// age can only be changed by this class's methods
private int age;
}
Person me = new Person();
me.name = "Geoffrey"; // This works
System.out.println(me.age); // This does not work
-
public
: the variable can be read or written by anyone -
private
: the variable can only read or written by methods defined on that class
> Click or hit Control-Enter to run Example.main above
Function Access Modifiers
public class Person {
public void printIt() {
System.out.println("It");
}
private void printYou() {
System.out.println("You");
}
}
Person me = new Person();
me.printIt(); // This works
me.printYou(); // This does not work
-
public
: the method can be called by anyone -
private
: the method can only be called by other methods on that class
> Click or hit Control-Enter to run Example.main above
Other Access Modifiers
(Java also provides protected
and package private modifiers—but they
don’t make sense until we discuss packages.)
Getters and Setters
public class Person {
private int age;
public void setAge(int setAge) {
this.age = setAge;
}
public int getAge() {
return this.age;
}
}
In Java it’s common to have private instance variables with public methods that set or get their values: called setters and getters.
Getters and Setters: Why?
public class Person {
private int name;
private int firstName;
private int lastName;
public void setName(int setName) {
String[] nameParts = setName.split(" ");
this.name = setName;
this.firstName = nameParts[0];
this.lastName = nameParts[1];
}
public int getName() {
return this.name;
}
public int getFirstName() {
return this.firstName;
}
public int getLastName() {
return this.lastName;
}
}
Getters and setters allow an object to react to changes to its variables.
Access Modifiers: Questions?
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.
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.
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
static
Variables
public class Course {
public static int count = 0;
public void printCount() {
System.out.println(this.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
> Click or hit Control-Enter to run Example.main above
static
, public
, and private
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
Object Modeling
We frequently use Java objects to model real objects or entities.
Objects allow us to design software that deals with things in realistic and natural ways.
Let’s Model Something
> Click or hit Control-Enter to run the code above
Announcements
-
TC 8 is out and due Sunday by midnight.
-
MP3 is out and due week from today. The early deadline is today at 5PM.
-
My office hours continue today at 11AM in the lounge outside of Siebel 0226.