// 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:
Employeeis 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. nextIdis a static variable that keeps track of the next available ID for a new employee.- The constructor initializes the
nameandageof the employee and assigns a unique ID to theIDvariable using thenextIdstatic variable. - The
showmethod prints the details of the employee (ID, name, and age). - The
showNextIdmethod prints the next available employee ID. - The
finalizemethod is called by the garbage collector before an object is reclaimed. In this case, it decrements thenextIdvariable.
2. HelloWorld Class:
• HelloWorldis the main class with themainmethod.- • Inside the
mainmethod:- • Two
Employeeobjects,EandF, are created with specific names and ages. Their details are then displayed using theshowmethod. - • A sub-block is created, where two additional
Employeeobjects,XandY, are created and displayed. - • The references
X,Y, andFare 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 thefinalizemethod is executed for objects pending finalization.- • The
showNextIdmethod of the originalEmployeeobjectEis 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