Constructors and Visibility
> Click or hit Control-Enter to run Example.main above
Review: Class Declaration
class Person {
// Variable declarations
String name;
// Declarations can include initialization
int age = 0;
// Any code has to go inside functions
void printName() {
System.out.println(this.name);
}
void birthday() {
age++;
}
}
Review: this
class Dimensions {
int width;
int height;
int area() {
return this.width * this.height;
}
}
Instance methods can refer to their instance variables using the this
keyword.
this
refers to the instance that is executing the method.
When to Use this
class Dimensions {
int width;
int height;
int area() {
return width * height;
}
}
This also works, and you might consider it a bit cleaner.
New Examples
> Click or hit Control-Enter to run Example.main above
> Click or hit Control-Enter to run Example.main above
Constructors
class Course {
String name;
// This is a constructor.
Course(String setName) {
this.name = setName;
}
}
When we create a new
object, a special method called a constructor is run.
-
Constructors are only run when the class is created. They can’t be called again.
Constructors: Syntax
public class Course {
String name;
// The constructor must be named Course
// It's not declared to return anything...
Course(String setName) {
this.name = setName;
// And doesn't explicitly return anything, but always returns a new Course
}
}
-
Constructors can and do perform class-specific initialization.
-
Constructors always shared the same same name as the class they create:
Course
in the example above. -
Constructors don’t declare or explicitly return anything—but always return a new instance of their class.
Constructors: Overloading
public class Course {
String name;
// This constructor sets the name
Course(String setName) {
this.name = setName;
}
// This constructor initializes the name to a blank string
Course() {
this.name = "";
}
}
Like other functions, constructors can be overloaded.
Constructors: this
public class Course {
String name;
// This constructor sets the name
Course(String setName) {
this.name = setName;
}
// This constructor initializes the name to a blank string
// by calling the other constructor
Course() {
this("");
}
}
Constructors can use this()
to call other constructors.
The Default Constructor
class Course {
String name;
}
// This is the same as
class Course {
String name;
Course() {
}
}
If you don’t define a constructor, it’s equivalent of an empty constructor that doesn’t set any fields.
Constructors Can’t Fail
class Course {
String name;
Course(String setName) {
// What do I do if the name is invalid?
this.name = setName;
}
}
Constructors must return a new instance of their class.
-
So we don’t have a good way to reject invalid inputs in constructors—yet.
-
We’ll come back to this when we talk about static methods…
-
and when we discuss exceptions.
> Click or hit Control-Enter to run Example.main above
Questions about Constructors?
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 quite yet…)
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(String 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?
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 Example.main above
Announcements
-
Congrats to those that have taken the midterm! If you haven’t yet, good luck.
-
My office hours as usual today from 1–3PM in Siebel 2227.
-
We have a anonymous feedback form to the course website. Use it to give us feedback!