Saturday, May 30, 2015

Variables


// SCOPE OF THE VARIABLE

// Instance Variable: Variables that are defined inside class are called instance variables.
// These variable have the access modifiers
// It may or may not be declare at the beginning

// Local Variable: Variables that are defined inside the methods are called local variables.
// They don't have the access modifiers
// It should be declared or initialized in the beginning eg: int i=0;

// Static Variable: Variables that are defined inside class with static keyword are called Static variables
//public static String name;
//This variable can be accessed using ClassName.staticVariableName and also by creating the object

public class ClassA {

static int symbolNo ;     // static variable
int rollNo ;       // instance variable

public static void main(String[] args) {

int localvar = 5; // local variable
System.out.println("local variable: "+localvar);

ClassA classA = new ClassA();
int instanceVar = classA.rollNo = 2;
System.out.println("instance variable: "+instanceVar);

ClassA.symbolNo = 130;
System.out.println("static variable: "+ClassA.symbolNo);


}
}


No comments:

Post a Comment