Professional Documents
Culture Documents
Object Construction
If no defined constructors in your class, the default constructor for the parent class is used. A default constructor is one with no parameters.
Even when you dont extend a class explicitly, implicitly
all classes inherit the Object class, the mother-of-all
classes.
Object has a lone default constructor Object(), which
will set instances variables to their default:
booleans to false
numeric values to 0
objects to null.
1
There is an important difference between fields (instance variables) and local variables.
You must always explicitly initialize local variables in a
method.
But if you dont initialize a field in a class, it is automatically initialized to a default (0, false, or null) depending
on its type.
Overloading
Often classes have multiple constructors. For example
the GregorianCalendar class had more than one constructor.
GregorianCalendar today = new GregorianCalendar();
or
GregorianCalendar ago = new GregorianCalendar(1968, 12,
1)
Note: The Calendar class declares a bunch of constants
for months and days of the week. Can use Calendar.MONDAY for Monday, Calendar.DECEMBER for
Decemeber.
7
For example, the String class has multiple valueOf methods, each with a different signature
valueOf(boolean)
valueOf(char)
valueOf(char[])
valueOf(char[], int, int)
valueOf(double)
valueOf(float)
valueOf(int)
valueOf(long)
valueOf(Object)
The return type is not part of the signature. You cant
have two methods with identical signatures and different return types.
Parameter Names
Coming up with parameter names for constructor and
methods can be difficult/tedious. Single character parameters are not the best.
A couple of conventions
Use a as a prefix
double x, y;
public double setXY(double aX, double aY) {
x = aX;
y = aY;
}
10
Another way
Parameters variables shadow instance variables with
the same name. That is, for the scope of the method
the local variable hides the name of the instance variable.
double x, y;
public double setXY(double x, double y) {
this.x = x;
this.y = y;
}
this is the implicit parameter. this refers to the current
object.
double)
11
}
}
public static void main(String[] args) {
Point p0 = new Point();
Point p1 = new Point(3, 5);
System.out.println("(" + p0.getX() + "," + p0.getY() + ")");
System.out.println("(" + p1.getX() + "," + p1.getY() + ")");
}
}
$ java Point
(0.0,0.0)
(3.0,5.0)
{
x = 9.9;
y = 99.9;
}
public static void main(String[] args) {
Point p0 = new Point();
Point p1 = new Point(3, 5);
System.out.println("(" + p0.getX() + "," + p0.getY() + ")");
System.out.println("(" + p1.getX() + "," + p1.getY() + ")");
}
}
$ java Point
(9.9,99.9)
(3.0,5.0)
import java.util.*;
public class RandNum {
static int rand;
static
{
Random random = new Random();
rand = random.nextInt(100);
}
public int getRand() {
return rand;
}
13
14
Destroying objects
When the reference count of an object reaches zero, it
is fair game for the garbage collector. But when the
garbage collector actually does this, who knows? If just
releasing memory, when the garbage collector releases
memory is mot that big of a deal. But when releasing something else, such as a file handle or network
connection need something else besides garbage collection. There is a finalize() method which is called
by the garbage collector to perform any last minute
clean-up. But as garbage collection may never occur,
such as on machines with plenty of memory, finalize
may never run. So cant guarantee it will work. Need
to call close or dispose or some other method.
15
Homework
import java.util.*;
16
//method chaining
//today.getTime() is a method from Calendar class
// (today.getTime()) returns a Date class
// representing the time in ms since Epoch
//Epoch is 1/1/1970 00:00:00 GMT
//Date.getTime() returns a long of the ms
//since the epoch
// method chains read left to right
System.out.println((today.getTime().getTime()
- lunar.getTime().getTime()) / MS_IN_A_DAY);
}
public static final int MS_IN_A_DAY = 1000 * 60 * 60 * 24;
}
17
static
static
static
static
static
static
static
static
static
native
java.lang.String
java.lang.String
java.lang.String
java.lang.String
java.lang.String
java.lang.String
java.lang.String
java.lang.String
java.lang.String
java.lang.String