Unit III
Unit III
Example :
//Java Program to create and call a default constructor
class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
Types of constructor
2.Parameterized constructor:
A constructor which has a specific number of parameters is called a parameterized
constructor.
Why use the parameterized constructor?
The parameterized constructor is used to provide different values to distinct objects.
However, you can provide the same values also.
Types of constructor
There is copy constructor in Java like c++,we can copy the values from one object to
another like copy constructor in C++.
There are many ways to copy the values of one object into another in Java. They are:
1. By constructor
2. By assigning the values of one object into another
3. By clone() method of Object class
Characteristics of constructor
Constructor overloading in Java is a technique of having more than one constructor with
different parameter lists.
They are arranged in a way that each constructor performs a different task.
They are differentiated by the compiler by the number of parameters in the list and their
types.
Dynamic initialization of object
• Dynamic initialization of object refers to initializing the objects at a run time i.e., the
initial value of an object is provided during run time.
• It can be achieved by using constructors and by passing parameters to the constructors.
• This comes in really handy when there are multiple constructors of the same class with
different inputs.
Dynamic initialization of object
Dynamic Constructor:
• The constructor used for allocating the memory at runtime is known as the dynamic
constructor.
• The memory is allocated at runtime using a new operator .
Symbolic constants
Symbolic constants are nothing more than a label or name that is used to represent a fixed
value that never changes throughout a program.
For example, one might define PI as a constant to represent the value 3.14159.
In other words, the value that cannot be changed.
How to declare constant in Java?
In Java, to declare any variable as constant, we use static and final modifiers.
It is also known as non-access modifiers.
Static and final modifier
In Java, when we create an object of the class it occupies some space in the memory
(heap).
If we do not delete these objects, it remains in the memory and occupies unnecessary
space that is not upright from the aspect of programming.
To resolve this problem, we use the destructor.
The destructor is the opposite of the constructor.
The constructor is used to initialize objects while the destructor is used to delete or
destroy the object that releases the resource occupied by the object.
Garbage collection:destructors
It is a special method that automatically gets called when an object is no longer used.
When an object completes its life-cycle the garbage collector deletes that object and
deallocates or releases the memory occupied by the object.
It is also known as finalizers that are non-deterministic.
In Java, the allocation and deallocation of objects handled by the garbage collector.
Advantages of Destructor
• It is a protected method of the Object class that is defined in the java.lang package.
• It can be called only once.
• We need to call the finalize() method explicitly if we want to override the method.
• The gc() is a method of JVM executed by the Garbage Collector. It invokes when the heap
memory is full and requires more memory for new arriving objects.
• Except for the unchecked exceptions, the JVM ignores all the exceptions that occur by the
finalize() method.