Computer Applications
Explain instance variables. Give an example.
Java Classes
ICSE 2008
30 Likes
Answer
Variables that are declared inside a class without using the keyword 'static' and outside any member methods are termed instance variables. Each object of the class gets its own copy of instance variables. For example, in the below class:
class Cuboid {
private double height;
private double width;
private double depth;
private double volume;
public void input(int h, int w, int d) {
height = h;
width = w;
depth = d;
}
public void computeVolume() {
volume = height * width * depth;
System.out.println("Volume = " + volume);
}
}
height, width, depth and volume are instance variables.
Answered By
15 Likes