In: Computer Science
Java question:
Explain how class (static) variables and methods differ from their instance counterparts. Give an example of a class that contains at least one class variable and at least one class method. Explain why using a class variable and method rather than an instance variable and method would be the correct choice in the example you select.
thanks for the question,
static class variables belongs to class and not objects of the class, There is only one instance of static variable that gets created per class, whereas we have a set of member variables for each object we create of that class type.
static methods can be invoked either using class name or object reference, but instance methods can only be invoked using object reference.
=============================================================================
We need to create static variables when we want to share data between objects of the class or want to have a value that remains unchanged and can be used by all objects for example constants like PI value or accelaration due to gravity which are constants, we dont need to create an instance of each constant for each object , this will allow us to save some memory consumption which can be avoided if we declare them static.
Below is the simple class Box where i have demonstrated the use of static member variable and method
==============================================================================
public class Box {
private int
length;
private int
width;
private int
height;
// static variable to track the number of box
objects created
// initialize the value to 0
private static int boxCount =
0;
// every time we create a Box object using the
default constructor
// we increment the boxCount by 1
public Box() {
length
= 0;
width =
0;
height
= 0;
// increment the box
count value by 1 everytime an object is created
boxCount += 1;
}
// similarly when the overloaded constructor
below is called
// we increment the boxCount by 1
public Box(int
length, int width, int height)
{
this.length = length;
this.width = width;
this.height = height;
// increment the box
count value by 1 everytime an object is created
boxCount += 1;
}
// returns the total number of box objects
created
public static int getBoxCount()
{
return
boxCount;
}
public int getVolume() {
return
getHeight() * getWidth() * getLength();
}
public int getHeight() {
return
height;
}
public int getWidth() {
return
width;
}
public int getLength() {
return
length;
}
public void
setLength(int length) {
this.length = length;
}
public void
setWidth(int width) {
this.width = width;
}
public void
setHeight(int height) {
this.height = height;
}
public static void
main(String[] args) {
// even when no box
object is created we can invoke the getBoxCount()
// method; because we
have declared it with static keyword
// the below line should
return 0
System.out.println("Total Box Objects:
" + Box.getBoxCount());
Box boxOne =
new Box();
// the below line should
return 1
System.out.println("Total Box Objects:
" + Box.getBoxCount());
Box bowTwo =
new Box(1, 2, 3);
// the below line should
return 2
System.out.println("Total Box Objects:
" + Box.getBoxCount());
}
}
========================================================================