Getters and Setters
> Click or hit Control-Enter to run Example.main above
Congratulations!
This class did well on the midterm. We’ll discuss more on Monday, including how to interpret your result…
-
Fall 2018: 73%
-
Spring 2019: 79%
-
Fall 2019: 82%
Review: Access Modifiers
public class Person {
public String name;
private int age;
private void printName() {
System.out.println(name);
}
public int getAge() {
return age;
}
}
Java provides ways to protect instance variables and methods. We refer to these as access modifiers:
Review: 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
Review: 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
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) {
age = setAge;
}
public int getAge() {
return 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(String setName) {
String[] nameParts = setName.split(" ");
name = setName;
firstName = nameParts[0];
lastName = nameParts[1];
}
public int getName() {
return name;
}
public int getFirstName() {
return firstName;
}
public int getLastName() {
return lastName;
}
}
Getters and setters allow an object to react to changes to its variables. It also allows us to create read- and write-only variables.
> Click or hit Control-Enter to run Example.main above
> Click or hit Control-Enter to run Example.main above
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(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(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
Announcements
-
My office hours as usual today from 1–3PM in Siebel 2227.
-
I’ll be doing one "talk to your professor" interview today at 2PM in Siebel 2227.
-
Good luck finishing up the MP1 early deadline! It’s due on your deadline day this weekend.
-
We have a anonymous feedback form to the course website. Use it to give us feedback!