Constructor GC
Constructor GC
Unit-2
Constructor
Garbage
By
Collection UMESH PRASAD ROUT
Constructor
● Constructor is a special method that creates(constructs) object.
Hence the name constructor.
● It initializes the object(instance), means it initializes the
instance(non-static) data members in the object.
● Speciality of constructor :
○ Its name is same as class name
○ It has no return type
○ It is not called explicitly like other methods
● Constructors can have arguments like normal methods.
● Constructors can have modifiers like normal methods.
2
Types of Constructor
● There are two type of constructor in Java
1. Default constructor/No-Argument Constructor
2. Parameterized Constructor
● If a java class has no constructor,compiler provides a default
constructor.
● But if there is any constructor defined by the programmer,
compiler does not provide any constructor.
3
Default Vs Parameterized Constructor
1. Default Constructor
● Has no argument/parameter
● Used to initialize object with identical values
● There can be only one default constructor in a class
2. Parameterized Constructor
● Has argument(s)/parameter(s)
● Used to initialize object with different values
● There can be many parameterized constructor in a
class
4
Default Constructor Demo Program
class Dcon public static void main(String []args)
{ {
System.out.print("Object 1 : ");
int a; Dcon ob1=new Dcon();
System.out.print("Object 2 : ");
Dcon() Dcon ob2=new Dcon();
{ }
a=7; }
System.out.println("a="+a); OUTPUT
} Object 1 : a=7
Object 2 : a=7
5
Parameterized Constructor Demo Program
9
Garbage Collection
● In Java, garbage means unreferenced objects.
● Garbage Collection is process of reclaiming the runtime unused memory
automatically.
● In other words, it is a way to destroy the unused objects.
● To do so, we are using free() function in C language and delete() in C++. But,
in java it is performed automatically. Hence, java provides better memory
management.
Advantage of Garbage Collection
● It makes java memory efficient because garbage collector removes the
unreferenced objects from heap memory.
● It is automatically done by the garbage collector(a part of JVM) so we don't
need to make extra efforts.
10
Ways of Object Un-referencing
1. By nulling a reference:
Student s=new Student();
s=null;
2. By assigning a reference to another:
Student s1=new Student();
Student s2=new Student();
s1=s2; //now the first object referred by s1 is available for garbage
collection
3. By anonymous object:
new Student(); 11
finalize() Method
● The finalize() method is called by default for every object just
before its deletion.
● This method can be used to perform cleanup activity before
destroying any object.
● It is defined in java.lang.Object class as:
protected void finalize(){ }
● It works the same way as the destructor in C++.
12
gc() Method
● The gc() method is used for requesting JVM to run the Garbage
Collector.
● The gc() is defined in java.lang.System class as:
public static void gc(){ }
● Call syntax:
System.gc()
13
Garbage Collection Demo Program
class GCDemo public void finalize()
{ {
public static void main(String args[]) System.out.println("Object destroyed");
{ }
GCDemo ob1=new GCDemo();
OUTPUT
GCDemo ob2=new GCDemo();
ob1=null;
Object destroyed
ob2=null;
Object destroyed
System.gc();
}
14