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

Online doctor, can you explain me the er diagram for the online doctor syst...

can you explain me the er diagram for the online doctor system

Write a program of else statement in java, Write a program of else statemen...

Write a program of else statement in java? The else statement in Java // This is the Hello program in Java class Hello { public static void main (String args[]) { if (

Describe the if statement in java, Describe the if statement in Java ? ...

Describe the if statement in Java ? All but the most trivial computer programs required to form decisions. They test a condition and operate in a different way based on the out

EJB in J2EE? , EJB 2.x is broadly adopted server side component architectu...

EJB 2.x is broadly adopted server side component architecture for J2EE. 1. EJB is a remote, distributed multi-tier system and allows protocols like IIOP, JRMP, and HTTP etc.

Advantages java layout managers over traditional windowing, What are the ad...

What are the advantages that Java's layout managers provide over traditional windowing systems ?

Make changes to an editable pdf form, Make Changes to an Editable PDF Form ...

Make Changes to an Editable PDF Form Project Description: I need for someone who is able to make some changes to PDF forms. Skills required: Data Processing, Data Entry

Constractar, Create a class HourlyWorker mind: particularHourlyWorker emplo...

Create a class HourlyWorker mind: particularHourlyWorker employee. • Declare two data members named wage and hours of double type with private access. • Implement a parameterized c

Develop an employee application, Employee application Project Descriptio...

Employee application Project Description: Subsequent Technology should be used 1) UI (jsp, jstl ,html, etc) 2) Framework (Spring MVC and hibernate) 3) Database (MYSQ

Mention five applications of artificial intelligent system, Mention any fiv...

Mention any five applications of Artificial Intelligent System? 1)Medical Diagnosis 2)Robots 3) Games 4) Business intelligence 5)Image Recognition.

Socket programming in Java: TCP, Objectives: socket programming in Java: TC...

Objectives: socket programming in Java: TCP Exercises: Goal: In this project we will develop a Web server in two steps. In the end, you will have built a multi-threaded Web ser

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