Give an example of code using static and finalize, JAVA Programming

Assignment Help:

Give an example of code using static and finalize

Example Code: using static & finalize ()

We want to count exact number of objects in memory of a Student class the one defined earlier. For this purpose, we will modify Student class.

Add a static variable countStudents which helps in maintaining the count of student objects.

Write a getter for this static variable. (Remember, getter also should be static one. Hoping so, you know the grounds).

In all constructors, write a code which will increment countStudents by one.

Override finalize() method and decrement countStudents variable by one.

Override toString() method.

Class Object is a superclass (base or parent) class of all the classes in java by default. It has already finalize() and toString() method (used to convert an object state into string). Thus we are actually overriding these methods over here.

 

By making all above modifications, student class will look like

 

// File Student.java

 

public class Student {

 

private String name;

private int rollNo;

private static int countStudents = 0;

 

// Standard Setters

public void setName (String name) {

this.name = name;

}

 

// Note the masking of class level variable rollNo

public void setRollNo (int rollNo) {

if (rollNo > 0) {

this.rollNo = rollNo;

}else {

this.rollNo = 100;

}

}

// Standard Getters

public String getName ( ) {

return name;

}

public int getRollNo ( ) {

return rollNo;

}

 

// gettter of static countStudents variable

public static int getCountStudents(){

return countStudents;

}

 

// Default Constructor public Student() {

name = "not set";

rollNo = 100;

countStudents += 1;

}

 

// parameterized Constructor for a new student

public Student(String name, int rollNo) {

setName(name); //call to setter of name

setRollNo(rollNo); //call to setter of rollNo

countStudents += 1;

}

// Copy Constructor for a new student

public Student(Student s) {

name = s.name;

rollNo = s.rollNo;

countStudents += 1;

}

// method used to display method on console

public void print () {

System.out.print("Student name: " +name);

System.out.println(", roll no: " +rollNo);

}

// overriding toString method of java.lang.Object class

public String toString(){

return "name: " + name + " RollNo: " + rollNo;

}

// overriding finalize method of Object class

public void finalize(){

countStudents -= 1;

}

} // end of class

 

 


Related Discussions:- Give an example of code using static and finalize

Explain data security management issue, Explain DATA SECURITY Management is...

Explain DATA SECURITY Management issue? All data in the system is extremely critical to its operation, the security of the customer's personal data is most important. Most of t

Programming help, writing a program to find common friends with the use of ...

writing a program to find common friends with the use of array

Difference between java mail and jms queue, JMS is the ideal high-performan...

JMS is the ideal high-performance messaging stage for intra business messaging, with full programmatic control over quality of service and delivery options. JavaMail gives lowes

Illustrate normalization? , Normalization is a design technique that is m...

Normalization is a design technique that is mostly used as a guide in designing relational databases. Normalization is necessary a two step process that gives data into tabular f

Javascript work, It could be something simple for a good js coder. But ther...

It could be something simple for a good js coder. But there may be a custom design and I can give more feedback for the person who is interested in working with me. Check Invisi

#title.swithcase, 2. Problem Description: A Java program needs to be writte...

2. Problem Description: A Java program needs to be written to display the result of following operations based input character. The table for input character with operations is : I

Package diagrams , To simplify complex class diagrams you may group classes...

To simplify complex class diagrams you may group classes into packages.

I need a dcs source code and jar, I need a DCS (Source code and JAR) to pro...

I need a DCS (Source code and JAR) to process packets from a cheap chinese GPS TK110, the unit is already communicating with the server but is not parsing. So I need someone with e

What is jsf life cycle and its phases, The series of steps followed by an a...

The series of steps followed by an application is known its life cycle. A JSF application typically follows six steps in its life. 1. Restore view phase 2. Apply request valu

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd