0% found this document useful (0 votes)
33 views

Unit III

The document discusses constructors and destructors in Java. It defines constructors as blocks of code similar to methods that are called when an instance of a class is created. It notes there are no destructors in Java, but rather a garbage collector automatically deletes unused objects to free up memory. The document outlines different types of constructors including default, parameterized, and copy constructors. It also covers the finalize() method in Java which works similar to a destructor by releasing resources when an object is destroyed.

Uploaded by

khudbuddinpatil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Unit III

The document discusses constructors and destructors in Java. It defines constructors as blocks of code similar to methods that are called when an instance of a class is created. It notes there are no destructors in Java, but rather a garbage collector automatically deletes unused objects to free up memory. The document outlines different types of constructors including default, parameterized, and copy constructors. It also covers the finalize() method in Java which works similar to a destructor by releasing resources when an object is destroyed.

Uploaded by

khudbuddinpatil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Unit III

CONSTRUCTORS AND DESTRUCTORS


introduction

 In Java , a constructor is a block of codes similar to the method. It is called when an


instance of the class is created.
 It is called constructor because it constructs the values at the time of object creation.
 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. In such case,
Java compiler provides a default constructor by default.
Types of constructor

 Types of constructor is as follows:


1. Non-parameterized constructor(Default constructor)
2. Parameterized constructor
Types of constructor

1. Non-parameterized constructor(Default constructor)


 A constructor is called "Default Constructor" when it doesn't have any parameter.
 Syntax of default constructor:
<class_name>()
{
}
Types of constructor

 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

 Example : public static void main(String args[]){


class Student4{ //creating objects and passing values
int id; Student4 s1 = new Student4(111,"Karan");
String name;
Student4 s2 = new Student4(222,"Aryan");
//creating a parameterized constructor
//
Student4(int i,String n){ calling method to display the values of object
id = i; s1.display();
name = n; s2.display();
} }
//method to display the values }
void display()
{System.out.println(id+" "+name);}
Java copy 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

1. Constructor name must be the same as its class name


2. A Constructor must have no explicit return type
3. A Java constructor cannot be abstract, static, final, and synchronized
4. A constructor must not have a return type.
5. The constructor is invoked implicitly.
6. The Java compiler provides a default constructor if you don't have any constructor in a
class.
7. The constructor name must be same as the class name.
Use of constructor

 A constructor is used to initialize the state of an object.


Constructor overloading

 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

• The purpose to use the static modifier is to manage the memory.


• It also allows the variable to be available without loading any instance of the class in
which it is defined.
• The final modifier represents that the value of the variable cannot be changed.
• It also makes the primitive data type immutable or unchangeable.
• Syntax:
static final datatype identifier_name=value;
• Example :
static final double PRICE=432.78;
Final keyword

 The final keyword in java is used to restrict the user.


 The java final keyword can be used in many context.
 Final can be:
1. variable
2. method
3. class
 The final keyword can be applied with the variables, a final variable that have no value it
is called blank final variable or uninitialized final variable.
Final keyword

1) Java final variable


 If you make any variable as final, you cannot change the value of final variable(It will be
constant).
2) Java final method
 If you make any method as final, you cannot override it.
3) Java final class
 If you make any class as final, you cannot extend it.
 Is final method inherited?
Ans) Yes, final method is inherited but you cannot override it.
Garbage collection:destructors

 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

 Remember that there is no concept of destructor in Java.


 In place of the destructor, Java provides the garbage collector that works the same as the
destructor.
 The garbage collector is a program (thread) that runs on the JVM.
 It automatically deletes the unused objects (objects that are no longer used) and free-up
the memory.
 The programmer has no need to manage memory, manually.
What is the destructor in Java?

 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 releases the resources occupied by the object.


• No explicit call is required, it is automatically invoked at the end of the program
execution.
• It does not accept any parameter and cannot be overloaded.
How does destructor work?

 When the object is created it occupies the space in the heap.


 These objects are used by the threads.
 If the objects are no longer is used by the thread it becomes eligible for the garbage
collection.
 The memory occupied by that object is now available for new objects that are being
created.
 It is noted that when the garbage collector destroys the object, the JRE calls the finalize()
method to close the connections such as database and network connection.
Java finalize () method

 It is difficult for the programmer to forcefully execute the garbage collector to


destroy the object.
 But Java provides an alternative way to do the same.
 The Java Object class provides the finalize() method that works the same as the
destructor.
 The syntax of the finalize() method is as follows:
protected void finalize throws Throwable()
{
//resources to be close
}
Java finalize () method

• 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.

You might also like