1
0

Instance Variable in Java

Objects need some way to keep track of their state. The way to allow an object to keep track of its state is to use instance variables. An instance variable can be any of the basic data types discussed in the previous topic, or it can even be another object. The variables are defined within the block (between the curly brackets) of the class.

A class basically consists of two sections. One declares the variables and the other declares the methods. These variables and methods are called instance variables and instance methods. This is because whenever a class is instantiated, every time a new copy of each of them is created. They can be accessed using the objects. The above example FirstClass has two instance variables age and salary. It also has two instance methods Data() and Show().

Leave a Reply