40-244 – Advanced Programming
PROGRAMMING IN
Lecture 9
The Kickoff
What We Will Learn
Constructors
Method Overloading
Member Initialization
Finalization and Garbage Collection
The Meaning of Static
Constructors
class Test {
public Test() {
…
}
}
…
Test t = new Test();
Guarantee Initialization of Every Object
Are Called Automatically
Have the Same Name as the Classes
Default Constructors
Many of them may exist for each class
Many Constructors per Class
class Test {
public Test() { }
public Test(int i) { }
public Test(String s) { }
}
Test t1 = new Test();
Test t2 = new Test(4);
Test t3 = new Test(“Java”);
Method Overloading
Makes your programs easier to read
Parameter list distinguishes methods
Overloading with primitives needs some
care (refer to TIJ p. 197)
See java.lang.String for a good example
Differs from overriding (we will see later)
We can’t overload on return values
this Keyword
In method bodies, this refers to the object
which is executing the method
public class Test {
private int count;
public void setCount(int cnt) {
this.count = cnt;
// count = cnt;
}
public void m(int j) {
this.setCount(j + 1); // setCount(j + 1);
}
}
this Application
public class X {
public void processY(Y arg) {
// do something
}
}
public class Y {
private X myFriend;
...
public void someMethod() {
myFriend.processY(this); // process me!
}
}
Calling Constructors from
Other Constructors
public class X {
private int i, j;
public X() {
i = j = 10;
}
public X(int initJ) {
this();
j = initJ;
}
}
// call X() constructor
matching parameters
Call to constructors must be the first statement
Cannot call constructors from other methods
Getting Inside
aLight
lightIsOn
switchOn
the switchOn code
is the same for
both objects
anotherLight
false
lightIsOn
switchOn
true
It's not wise to replicate the code for each object
Getting Inside
aLight
switchOn
lightIsOn
false
anotherLight
Methods of the
Light class
lightIsOn
false
switchOn
How
can differentiate between
aLight.switchOn() and anotherLight.switchOn() ?
The Hidden Parameter
public void switchOn() {
lightIsOn = true;
}
The code may
look like this!
switchOn(this)
this.lightIsOn true
aLight.switchOn();
8F930534 is the
address of aLight!
CALL switchOn(8F930534)
this is the hidden parameter to every method body
The Meaning of static
static methods are those that don't need this
parameter
So they cannot reference fields
(e.g. this.lightIsOn)
But there may be static fields in a class
Static Field Example
public class Test {
static private int instanceCount = 0;
public Test() {
// initialize the object
instanceCount++;
}
static int getInstanceCount() {
return instanceCount;
}
}
Usage:
i = Test.instanceCount();
i = aTestObject.instanceCount();
Initialization
Various places to initialize:
Constructors
Member initializations
Instance initialization block
Static member initializations
Static initialization block
Member Initialization
Using variable initializers for fields
public fieldName = <right hand side>;
<right hand side> may contain
expressions, involving
literals
method calls
object creations
reference to other fields
Static members can be initialized in the same way
Static Initialization Block
What to do when we have to compute
something for static initialization?
public class Test {
static private int instanceCount;
static {
instanceCount = 0;
}
public Test() {
// initialize the object
instanceCount++;
}
// other methods
}
Instance Initialization Block
public class Test {
int i, j, k;
{
i = 10;
j = 5;
}
public Test() {
k = 102;
}
// other methods
}
Application in anonymous inner classes
Order of Initializations
Upon first use of a class
•
•
•
static fields
static initialization block
are executed in textual order
Upon object creation
fields
instance initialization block
are executed in textual order
finally the constructor is executed
Garbage
Light aLight = new Light();
aLight = null;
aLight
null
The created object is not accessible anymore
Such an object is called garbage
Garbage
Light aLight = new Light();
aLight = new Light();
aLight
The first object is now garbage
Garbage
Other causes of making garbage:
Going out of scope
Garbage chains!
aRef
Garbage
Garbage Collection
In C++ you must take care of all objects you
create (explicitly delete them)
In Java there is no explicit deletion
When VM goes out of memory, it starts
garbage collection
Garbage are identified and returned to the
free space
Finalizers
There is a special method
void finalize()
Originally defined in Object class, which you
can override it for your classes
It is called before space used by an object is
reused
Not called automatically for non-garbage
© Copyright 2025 Paperzz