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(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) {
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) {
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) {
name = setName;
}
// This constructor initializes the name to a blank string
Course() {
name = "";
}
}
Like other functions, constructors can be overloaded.
Constructors: this
public class Course {
String name;
// This constructor sets the name
Course(String setName) {
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?
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?
Announcements
-
Congrats to those that have taken the midterm! If you haven’t yet, good luck.
-
My office hours as usual today from 4–5PM in Siebel 2227.
-
We have a anonymous feedback form to the course website. Use it to give us feedback!