0% found this document useful (0 votes)
4 views14 pages

Constructor GC

Uploaded by

banitasatapathy8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

Constructor GC

Uploaded by

banitasatapathy8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Constructor in Java

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

class Pcon public static void main(String []args)


{ {
System.out.print("Object 1 : ");
int a; Pcon ob1=new Pcon(9);
System.out.print("Object 2 : ");
Pcon(int x) Pcon ob2=new Pcon(7);
{ }}
a=x; OUTPUT
System.out.println("a="+a);
Object 1 : a=9
}
Object 2 : a=7
6
Constructor Overloading
● A class can have multiple constructor.
● If a class has N number of constructor then, there is N ways of
creating object.
● If a class has multiple constructor, then the constructor is said
to be overloaded.
● Like method overloading all these constructor have same
name but different signatures.
● Many library classes have multiple constructor like
String,Scanner,Thread etc.
7
Constructor Overloading Demo Program
class Mcon Mcon(int a)
{ {
int x; x=a;
int y; y=a;
Mcon() System.out.println("Sum="+(x+y));
{ }
x=7; Mcon(int a, int b)
y=9; {
System.out.println("Sum="+(x+y)); x=a;
} y=b;
System.out.println("Sum="+(x+y));
}
8
Constructor Overloading Program Cont.
public static void main(String []args) OUTPUT
{
Mcon ob1=new Mcon(); Sum=16
Mcon ob2=new Mcon(5); Sum=10
Mcon ob3=new Mcon(6,9); Sum=15
}
}//end of class

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

You might also like