Joseph Haugh
University of New Mexico
void main() {
IO.println("Hello, world!");
}
int square(int x) {
return x * x;
}
void main() {
IO.println(square(5));
}
class Main {
void main() {
IO.println("Hello, world!");
}
}
Compact (what you wrote)
int square(int x) {
return x * x;
}
void main() {
IO.println(square(5));
}
Full (what Java sees)
class Main {
int square(int x) {
return x * x;
}
void main() {
IO.println(square(5));
}
}
int, double, String, int[]x and a y coordinate| Object | State | Behavior |
|---|---|---|
| Dog | name, breed, age | bark, eat, fetch |
| BankAccount | owner, balance | deposit, withdraw |
| Point | x, y | distanceTo, move |
| Student | name, GPA, major | enroll, graduate |
Dog class describes what a dog isDogDog, but with different stateclass ClassName {
// Fields: the data the object holds
<<type>> fieldName;
// Constructor: how to create a new object
ClassName(<<type>> param1, <<type>> param2) {
// initialize fields
}
// Methods: what the object can do
<<type>> methodName(<<type>> param) {
// method body
}
}
x and y coordinateclass Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
x and y are called fields (also called instance variables)Point object will have its own x and yclass Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
this Keywordclass Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
this refers to the current objectthis.fieldName lets you distinguish themthis.x = x means: set this object’s x field to the parameter xclass Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
void main() {
Point p = new Point(3, 7);
IO.println(p.x); // 3
IO.println(p.y); // 7
}
new to create an object — this calls the constructorobject.fieldclass Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
void main() {
Point p1 = new Point(3, 7);
Point p2 = new Point(-1, 0);
// Each object has its own independent data
IO.println(p1.x); // 3
IO.println(p2.x); // -1
// Changing one does not affect the other
p1.x = 99;
IO.println(p1.x); // 99
IO.println(p2.x); // -1
}
class Point {
int x;
int y;
// constructor ...
double distanceTo(Point other) {
int dx = this.x - other.x;
int dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
}
class Point {
int x;
int y;
// constructor ...
double distanceTo(Point other) {
int dx = this.x - other.x;
int dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
}
void main() {
Point origin = new Point(0, 0);
Point p = new Point(3, 4);
// Call the method
double d = origin.distanceTo(p);
IO.println(d); // 5.0
}
Instance methods are called using dot notation:
<<objectVariable>>.<<methodName>>(<<args>>)This is the same dot notation you’ve seen with Strings: s.length(), s.charAt(0)
class Point {
int x;
int y;
// constructor ...
double distanceTo(Point other) {
int dx = this.x - other.x;
int dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
}
origin.distanceTo(p):
origin is the object the method runs ondistanceTo, this refers to originother is the Point pclass BankAccount {
String owner;
double balance;
BankAccount(String owner, double balance) {
this.owner = owner;
this.balance = balance;
}
}
class BankAccount {
String owner;
double balance;
BankAccount(String owner, double balance) {
this.owner = owner;
this.balance = balance;
}
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
balance -= amount;
}
double getBalance() {
return balance;
}
}
void main() {
BankAccount account = new BankAccount("Alice", 1000.0);
account.deposit(500.0);
account.withdraw(200.0);
IO.println(account.getBalance()); // 1300.0
}
balance fieldvoid main() {
BankAccount alice = new BankAccount("Alice", 1000.0);
BankAccount bob = new BankAccount("Bob", 500.0);
alice.deposit(200.0);
bob.withdraw(100.0);
// Alice and Bob each have their own balance
IO.println(alice.getBalance()); // 1200.0
IO.println(bob.getBalance()); // 400.0
}
alice and bob are separate objects — changing one does not affect the otherRight now anyone can directly modify the fields of our objects:
BankAccount account = new BankAccount("Alice", 500.0);
account.balance = -99999; // Uh oh!This is dangerous — you might set a balance to an invalid value
Access modifiers let you control who can access fields and methods:
public — anyone can access itprivate — only code inside the class can access itclass BankAccount {
private String owner;
private double balance;
public BankAccount(String owner, double balance) {
this.owner = owner;
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
}
}
public double getBalance() {
return balance;
}
}
void main() {
BankAccount account = new BankAccount("Alice", 500.0);
account.balance = -99999; // COMPILE ERROR! balance is private
account.deposit(100.0);
IO.println(account.getBalance()); // 600.0
}
private forces all access to go through methodsprivate, you often provide a getter method to read the value:class Point {
private int x;
private int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
int getX() { return x; }
int getY() { return y; }
}
void main() {
Point p = new Point(3, 7);
IO.println(p.getX()); // 3
IO.println(p.getY()); // 7
}
void main() {
Point p = new Point(3, 7);
IO.println(p); // Prints something like: Point@3a71f4dd
}
toString methodclass Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() { return x; }
public int getY() { return y; }
public String toString() {
return "(" + x + ", " + y + ")";
}
}
void main() {
Point p = new Point(3, 7);
IO.println(p.toString()); // (3, 7)
IO.println(p); // (3, 7) -- Java calls toString automatically!
}
IO.println, Java automatically calls toString on ittoString is a great habit — it makes debugging much easierclass Student {
private String name;
private double gpa;
private int year; // 1 = freshman, 4 = senior, etc
public Student(String name, double gpa, int year) {
this.name = name;
this.gpa = gpa;
this.year = year;
}
}
class Student {
private String name;
private double gpa;
private int year;
public Student(String name, double gpa, int year) {
this.name = name;
this.gpa = gpa;
this.year = year;
}
public String getName() { return name; }
public double getGpa() { return gpa; }
public int getYear() { return year; }
public boolean isHonorsEligible() {
return gpa >= 3.5;
}
public void advanceYear() {
if (year < 4) {
year ++;
}
}
public String toString() {
return name + " (Year " + year + ", GPA: " + gpa + ")";
}
}
void main() {
Student s1 = new Student("Alice", 3.8, 1);
Student s2 = new Student("Bob", 2.9, 3);
IO.println(s1); // Alice (Year 1, GPA: 3.8)
IO.println(s2); // Bob (Year 3, GPA: 2.9)
IO.println(s1.isHonorsEligible()); // true
IO.println(s2.isHonorsEligible()); // false
s1.advanceYear();
IO.println(s1); // Alice (Year 2, GPA: 3.8)
}
Key Terms
this — refers to the current objectnew — creates an objectprivate/public — controls accessClass Structure
class Name {
<<accessMod>> <<type>> field;
<<accessMod>> Name(type param) {
this.field = param;
}
<<accessMod>> <<type>> getField() {
return field;
}
<<accessMod>> String toString() {
return "...";
}
}