Constructors This Keyword Garbage

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Constructors

S.REVATHI AP/CSE
Constructors in Java

In Java, a constructor is a block of codes similar to the


method.

It is called when an instance of the class is created.

At the time of calling constructor, memory for the


object is allocated in the memory.
It is a special type of method which is used to initialize the object.

Every time an object is created using the new() keyword, at least

one constructor is called.

It calls a default constructor if there is no constructor available in

the class.
Rules for creating Java constructor

Constructor name must be the same as its class name

A Constructor must have no explicit return type


Types
No-argument constructor (Default)
Parameterized constructor

Note: It is called constructor because it constructs the values at the time of object

creation. It is not necessary to write a constructor for a class. It is because java

compiler creates a default constructor if your class doesn't have any.


Java Default Constructor

A constructor is called "Default Constructor" when it doesn't

have any parameter.


Syntax:
<class_name>()
{
}
Example
If there is no constructor in a class, compiler automatically creates a default constructor.
Example
Java Parameterized Constructor

A constructor which has a specific number of parameters is

called a parameterized constructor.

The parameterized constructor is used to provide different

values to distinct objects.


this keyword

The this keyword refers to the current object in a method or

constructor.

The most common use of the this keyword is to eliminate the

confusion between class attributes and parameters with the

same name
Example
Java 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.

free() function in C language and delete() in C++. But, in java it is

performed automatically.
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.


How can an object be unreferenced?

By nulling the reference


By assigning a reference to another
By anonymous object etc
1) By nulling a reference:

Employee e=new Employee();

e=null;

2. By assigning a reference to another:

Employee e1=new Employee();

Employee e2=new Employee();

e1=e2;//
now the first object referred by e1 is available for garbage collection
3) By anonymous object:

new Employee();
finalize() method

The finalize() method - Garbage Collector always calls just before the
deletion/destroying the object which is eligible for Garbage Collection

To perform clean-up activity. Clean-up activity

Closing the resources associated with that object like Database Connection,
Network Connection or we can say resource de-allocation.

Not a reserved keyword.

Once the finalize method completes immediately Garbage Collector destroy that
object.
Syntax:

protected void finalize throws


Throwable{}
Why and How?
finalize() method releases system resources before the garbage
collector runs for a specific object.
JVM allows finalize() to be invoked only once per object.

In our class clean-up activities are there, then we have


to override this method to define our own clean-up activities.
protected void finalize() throws Throwable

//Keep some resource closing operations

here

You might also like