xxxxxxxxxx
public class Course {
private String name;
public Course(String setName) {
this.name = setName;
}
public void printName() {
System.out.println(this.name);
}
}
public class Example {
public static void main(String[] unused) {
Course CS125 = new Course("CS 125");
CS125.printName();
}
}
Constructors and Visibility
> Click or hit Control-Enter to run Example.main above
Happy Valentine’s Day
(Don’t expect your human relationships to be like your computer relationship.)
Q3 Review: Functions
xxxxxxxxxx
// What would be a small improvement to the function shown below?
static boolean a(int[] b) {
for (int i = 0; i < b.length; i++) {
for (int j = i; j < b.length; j++) {
if (b[j] < b[i]) {
return false;
}
}
}
return true;
}
System.out.println(a(new int[] { 5, 4, 3 }));
System.out.println(a(new int[] { 1, 2, 3 }));
> Click or hit Control-Enter to run the code above
Q3 Review: Functions
xxxxxxxxxx
// What is one problem with the function shown below?
static int s(int[][] a) {
int count = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
count += a[i][j];
}
}
return count;
}
System.out.println(s(new int[][] { { 0, 1 }, { 4, 5 } }));
> Click or hit Control-Enter to run the code above
Q3 Review: Function Terminology
What do we call the combination of the function name and list of argument types that is used by Java to determine which function to call?
(Everyone got credit for this because it wasn’t written on a slide, although it is now.)
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.
New Examples
xxxxxxxxxx
// Just a warning that some examples now work differently
// When run the main method of the Example class below is executed
public class Example {
public static void main(String[] unused) {
System.out.println("Hello, world!");
}
}
> 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: 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.
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.
xxxxxxxxxx
public class Person {
}
public class Example {
public static void main(String[] unused) {
Person you = new Person();
}
}
> 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
xxxxxxxxxx
public class Person {
public String name;
private int age;
}
public class Example {
public static void main(String[] unused) {
Person me = new Person();
me.name = "Geoffrey"; // This works
System.out.println(me.age); // This does not work
}
}
> 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
xxxxxxxxxx
public class Person {
public void printIt() {
System.out.println("It");
}
private void printYou() {
System.out.println("You");
}
}
public class Example {
public static void main(String[] unused) {
Person me = new Person();
me.printIt(); // This works
me.printYou(); // This does not work
}
}
> Click or hit Control-Enter to run Example.main above
Announcements
-
TC 8 will be out today and due Sunday by midnight. It’s the first set of object-related Turing’s Craft exercises.
-
MP3 is out and due a week from Friday. It’s your introduction to object-oriented programming, so you’ll be learning what you need to complete it in lecture and lab this week.
-
My office hours continue today at 11AM in the lounge outside of Siebel 0226.