// Java Program to count number of employees working in a company (Completed With Garbage Collection)
class Employee {
private String name;
private int age;
private int ID;
private static int nextId = 1;
public Employee (String name, int age) {
this.name = name;
this.age = age;
this.ID = nextId++;
}
public void show()
{
System.out.println( "\nID=" + ID + "\nName=" + name + "\nAge=" + age);
}
public void showNextId()
{
System.out.println("Next employee id will be=" + nextId);
}
protected void finalize()
{
--nextId;
// In this case,
// gc will call finalize()
// for 2 times for 2 objects.
}
}
class HelloWorld {
public static void main(String[] args) {
Employee E = new Employee("Ayush", 28);
Employee F = new Employee("Abhinav", 24);
E.show();
F.show();
//Sub block
{
Employee X = new Employee("GFG4", 23);
Employee Y = new Employee("GFG5", 21);
X.show();
Y.show();
X = Y = F = null;
System.gc();
System.runFinalization();
}
E.showNextId();
}
}
This Java program defines two classes: Employee
and HelloWorld
. It demonstrates the use of garbage collection in Java to manage object destruction and resource reclamation.
Here's a breakdown of the code:
1. Employee Class:
Employee
is a class representing employees in a company.- It has private instance variables
name
,age
, andID
, representing the employee's name, age, and a unique identifier. nextId
is a static variable that keeps track of the next available ID for a new employee.- The constructor initializes the
name
andage
of the employee and assigns a unique ID to theID
variable using thenextId
static variable. - The
show
method prints the details of the employee (ID, name, and age). - The
showNextId
method prints the next available employee ID. - The
finalize
method is called by the garbage collector before an object is reclaimed. In this case, it decrements thenextId
variable.
2. HelloWorld Class:
• HelloWorld
is the main class with themain
method.- • Inside the
main
method:- • Two
Employee
objects,E
andF
, are created with specific names and ages. Their details are then displayed using theshow
method. - • A sub-block is created, where two additional
Employee
objects,X
andY
, are created and displayed. - • The references
X
,Y
, andF
are set tonull
, indicating that there are no more references to those objects. - • The
System.gc()
method is called, suggesting to the JVM that it's an appropriate time to run the garbage collector. • System.runFinalization()
is called to ensure that thefinalize
method is executed for objects pending finalization.- • The
showNextId
method of the originalEmployee
objectE
is called to display the next available employee ID.
- • Two
Note: The use of System.gc()
and System.runFinalization()
is generally discouraged, and it's usually up to the JVM to decide when to run the garbage collector. Explicitly invoking these methods is not a recommended practice in most cases. The finalize
method is also not guaranteed to be called promptly by the garbage collector, and it's generally advised to use other resource management techniques.
Comments
Post a Comment