Initialization and Clean up
Advanced Programming
SUT • Spring 2019
Outline
-
Init methods
-
Constructors
-
No Destructor
-
Initialization
-
Cleanup
Initialization
Initialization
-
An instantiated object, is not a ready object
-
It may be and invalid object
-
Person p = new Person();
-
p is an object without name, id and, …
-
p is an invalid object
-
-
It should be initialized
Example
public class Student {
//Mandatory
private String name;
private long id;
//Optional
private String homepage;
public void setName(String name) {
if (name != null && !"".equals(name.trim()) &&
name.matches("[a-zA-Z ]+"))
this.name = name;
}
public void setId(long id) {
if (id > 10000000 && id < 100000000)
this.id = id;
}
public void setHomepage(String homepage) {
homepage = homepage;
}
}
Initialization Method
public void init(String name, long id) {
setName(name);
setId(id);
}
Using the Object
public static void main(String[] args) {
Student st = new Student();
// st is an invalid object now
st.init("Hossein Alizadeh", 45205068);
// st is initialized now. ready to be used
System.out.println(st.getName());
System.out.println(st.getId());
}
Circle c = new Circle();
c.init(12);
Book b1 = new Book();
b1.init(“من او”, “رضا اميرخانی”);
Book b2 = new Book();
b2.init(“شاهنامه”, “ابوالقاسم فردوسی”);
init() Method
-
What are the disadvantages of init() method?
-
Init method is invoked manually
-
There is no guarantee for init invocation
-
Before calling init method, the object has an invalid state
Constructors
-
Constructor is a special method
-
With the same name as the class
-
Without any return type
-
A constructor is called when an object is instantiated
-
No invalid object
-
Constructor example
public class Circle {
private double radius;
public Circle(double r) {
radius = r;
}
public double getArea() {
return this.radius * this.radius * 3.14;
}
public static void main(String[] args) {
Circle c = new Circle(2);
System.out.println(c.getArea());
}
}
Default Constructor
-
Constructors may have parameters
-
Default constructor: no parameter
-
Is implicitly implemented
-
You can write your own default-constructor
-
If you write any constructor, default implicit constructor is vanished.
-
Array Samples
public class Circle {
private double radius;
public Circle(double r) {
radius = r;
}
public Circle() {
System.out.println("Default Constructor");
}
public double getArea() {
return this.radius * this.radius * 3.14;
}
public static void main(String[] args) {
Circle c = new Circle(2);
System.out.println(c.getArea());
}
}
Default Constructor
Constructor
Destructor
-
Java needs no destructor
-
Destructor method in C++
-
Java has a finalize() method
-
You can implement it for your class
Finalize method
-
Java has no destructor
-
Java has a special method: finalize
-
finilize() is called when the object is garbage-collected
-
If garbage collector is not invoked
-
finalize() method is not called
-
-
Why we may need finalize?
-
Garbage collection is only about memory
Example
public class Circle {
private double radius;
public Circle(double r) {
radius = r;
}
public String toString() {
return "Circle [radius=" + radius + "]";
}
public void finalize() throws Throwable {
System.out.println("Finalize: " + toString());
}
public static void main(String[] args) {
f();
System.gc();
}
private static void f() {
Circle c = new Circle(2);
System.out.println(c);
}
}
Title Text
Initialization and Clean up
By Behnam Hatami
Initialization and Clean up
Initialization and Clean up / Advanced Programming Course @ SUT, Spring 2019
- 1,104