Explain the following variables with example
1, static variable
2, object variable
3, instance variable
Static variable:
A static variable in java is a variable which belongs to the class and initiated only once at the time of starting of the execution of the program. It belongs to the class not to the objects. In this, the single copy can be shared by the all the instances of the class and it can be access by the class name.
static int b;
Object variable:
An object is a kind of container which holds the references of the specific instance of the class. It is also known as the "variable" or "member".
public class Test {
public Test(String test) {
// constructor
System.out.println("Passed Name is :"+test);
}
public static void main(String []args) {
//create an object Test
Test myTest = new Test( "test" );
}
}
Instance variable:
Instance variable is kind of non-static variable which can be declared inside the class, outside the class, method or in a blocks.
class Test
{
// Static variable
static int a;
// Instance variable
int b;
}
Comments
Leave a comment