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

Program to built canvas printing calculator, Printing Calculator Canv...

Printing Calculator Canvas Printing Calculator In this assignment we will continue to work on our canvas printing web service. Specifically we will make use of Javascrip

Getting code, can i have code on this assignment: The code in the main meth...

can i have code on this assignment: The code in the main method should do the following: 1. Prompt the user for a string input value for the amount in dollars and store it in a va

Write complete application to play game tic-tac-toe, (Tic-Tac-Toe)  Create...

(Tic-Tac-Toe)  Create class TicTacToe that will enable you to write a complete application to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array o

Just i can told u looking for that for 6 mounth help me plea, how can i add...

how can i add script code in anouncement of a channel in a massneger the massnegr suport that but do not published it please help me please:((((((((((((((((((((((((((((((((((((((((

Explain subclasses and polymorphism, Explain Subclasses and Polymorphism ? ...

Explain Subclasses and Polymorphism ? Car and Motorcycle are subclasses of MotorVehicle. If you instantiate a Car or a Motorcycle along with new, you can use in which object a

Give an example of code using static and finalize, Give an example of code ...

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 de

What do you mean by java virtual machine?, Question 1 What do you mean by ...

Question 1 What do you mean by Java Virtual Machine? Question 2 Write a simple Java program to display a string message and explain the steps of Compilation and execution in Jav

Program implement class which have main string method, Implement the Money ...

Implement the Money class discussed in class. This class should represent a dollar and cents amount with 0-99 cents and the cents being the same sign as the dollars. The class shou

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