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

Area under curves, Write a program to find the area under the curve y = f(x...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

A simple desktop software, A simple Desktop Software Project Description...

A simple Desktop Software Project Description: We want a tool that could convert Flash Games into HTML5 Files. These are not tedious games, very simple game. I have attached

Are constructors inherited, Are constructors inherited ? Can a subclass ca...

Are constructors inherited ? Can a subclass call the parent class constructor and When?

Method to define the packages in java programme, Q. Write the method to def...

Q. Write the method to define the packages in java programme. Explain. Ans. Package: When we work on a project we have to break our programme in several classes. To organize

What are the services given by the rmi object, RMI Object services: In...

RMI Object services: In addition to its remote object architecture, RMI gives some basic object services, which may be needed in a distributed application. 1.  Object namin

Explain rmi, RMI is a set of APIs that permits to build distributed applica...

RMI is a set of APIs that permits to build distributed applications. RMI uses interfaces to describe remote objects to turn local method invocations into remote method invocations.

What is the importance of static variable, What is the importance of static...

What is the importance of static variable? Static variables are class level variables where all objects of the class refer to the similar variable. If one object alters the val

Describe graphics objects, Describe Graphics Objects ? In Java all draw...

Describe Graphics Objects ? In Java all drawing takes place via a Graphics object. This is an example of the class java.awt.Graphics. Initially the Graphics object you use w

Educational app, CP5307 Assessment Task 3 Coding Project Specification SP22...

CP5307 Assessment Task 3 Coding Project Specification SP22, 2019 Brisbane Description Another type of app found on app stores are “educational games”. There are immersive full-

data integrity - security component, Data integrity helps to make sure if ...

Data integrity helps to make sure if something is communicate and not tampered with in the mean while when transmission take place. Checksums: Simply inserts the bytes withi

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