You are on page 1of 22

Destructors

&
Package in Java
Destructor
Destructor in programming
• A destructor, in programming, is a special method or function within a
class that is automatically invoked when an object is destroyed or life
time of object ends.
• Purpose: Managing memory and resources
• In languages like C++ and Python, destructors are used to deallocate
memory, close file handles, release network connections, or perform
any other necessary cleanup tasks associated with an object.
• If System resources are not properly managed it will have impact on
system performance
Object life cycle
• Creation: Constructor
• Usage: Object methods
• Destruction: Garbage Collection
Garbage Collection

• A component of the Java Virtual Machine (JVM) responsible for


managing memory and reclaiming memory occupied by objects that
are no longer in use.
• System.gc(); // Request garbage collection
Unreferenced objects
Garbage Collection

• System.gc(); // Request garbage collection


• System.gc() is a method in Java that invokes garbage collector which
will destroy the unreferenced objects.
• It's important to note that manual manipulation of garbage collection
behavior is generally discouraged, as modern JVMs are highly
optimized and capable of managing memory effectively without
explicit intervention.
Case1:Object set to null

Object that are eligible for garbage


collection
Case2:One object reference is assigned to
another object

Object that are eligible for garbage


collection
Finalize() method
• Garbage collector always invokes finalize() method in java to perform
clean-up activities before destroying any object.
• After determining the objects that have no links or references, it calls
the finalize() method which will perform the clean up activity and the
garbage collector destroys the object.
• Cleanup activity is the process of closing all the resources being used
by an object before it is destroyed.
• Resources that can be used by any object are database connections,
network connections, etc. These resources are revoked back from the
unreferenced objects.
Garbage collection and finalize() method
public class Student
{
public static void main(String[] args)
{
Student s1 = new Student();
s1 = null;
System.gc();
System.out.println("Garbage collector is called");
}
}
Packages in Java
Packages in Java
• In Java, a package is a way to organize classes for easier management
and access.
• It helps in preventing naming conflicts and provides a clear structure
to your codebase.
• Its possible to create package inside another package
• In Java, a package is a way to bundle together related classes
Types of packages in Java
Built-in Packages example
• Java provides a vast array of predefined packages for various
functionalities like I/O, collections,databases etc.
• import java.util.Scanner;
Java.util built-in package
• import java.util.Date;
• import java.util.*;
• import java.io.FileReader;
• import java.io.FileWriter; Java.io Package
Creating custom packages
• Developers can create their own packages to organize their code
logically.
• Use package statement at the beginning of Java files.
• Choose meaningful package names reflecting functionality.
Import a class from custom package into your
code
Custom packages
• There may be two classes with the same name in two packages,
• Examples like Employee class is present in both packages
• college.staff.cs.Employee Employee class inside cs package

• college.staff.ee.Employee Employee class inside ee package


Summary

• Packages are essential for organizing and maintaining Java projects.


• They promote modularity, encapsulation, reusability, and
organization.
• Understanding package structure and usage is crucial for Java
developers to build scalable and maintainable applications.
Activity based on destructor
Create a class Employee

salary is instance variable of Employee class

define a parameterized constructor which initialize the salary of employee

Define main method

Create two objects e1 and e2 and pass the salary to the constructor

now display salary in e1


display salary in e2

now assign e1 to e2 like that e1=e2;


now display salary in e1
display salary of e2 compare it with the result before e1=e2

You might also like