Friday, 31 May 2013

Variables in JAVA

Variable is place where we maintain data inside Object.A variable is a container that holds values that are used in a Java program. To be able to use a variable it needs to be declared. Declaring variables is normally the first thing that happens in any program.

The basic form of a variable declaration is type identifier [ = value][, identifier [= value] ...] ;

type represents data type like it may int, float,long,char, byte etc.

identifier represents the name of variable.
EX:int a, b, c;         // declares three ints, a, b, and c.
int d = 3, e, f = 5; // declares three more ints, initializing d and f.
byte z = 22;         // initializes z.
double p = 3.14159; // declares an approximation of p.
char x = 'x';        // the variable x has the value 'x'.

Following are the types of variables in JAVA.

  • Static/Class Variable
  • Instance Variable
  • Local Variable
Static variable are termed as attributes of class.
JVM allocates memory in method area for static variables. 

Instance variables are termed as attributes of instance.
JVM allocates memory in heap memory for instance variables.

Local variables are termed as attributes of method (or) attributes used in control flow of statements.
JVM allocates memory in stack memory for local variables.

See the following example:

*******************************************

package com.sample.java.testing;

public class StaticInstanceVariableTest {

public int a;
public static int b;

public StaticInstanceVariableTest() {//default constructor
++a;
++b;

// a is initialized for every instance so it prints 1 every time

System.out.println("Instance variable is " + a);

// b is static variable so it is not initialized for every instace. So
// it incremented three times
System.out.println("Static variable is" + b);
}

public void addNumbers(int c, int d) {
// Here e is local variable declared inside method.
// c and d are parameters of the method
int e = c + d;
System.out.println("Addition of two numbers is " + e);
}

public static void main(String s[]) {
StaticInstanceVariableTest instanceVariableTest1 = new
  StaticInstanceVariableTest(); //called default constructor
StaticInstanceVariableTest instanceVariableTest2 = new
  StaticInstanceVariableTest(); //called default constructor
StaticInstanceVariableTest instanceVariableTest3 = new 
  StaticInstanceVariableTest(); //called default constructor

// we can access variable b with class name as it is static variable.

System.out.println("static variable value is incremented thrice" +
  StaticInstanceVariableTest.b);

// we can access variable a with instances of StaticInstanceVariableTest as it  
  is instance variable.
System.out.println("instance variable value is initialized for every instance "
  + instanceVariableTest1.a);
System.out.println("instance variable value is initialized for every instance "
  + instanceVariableTest2.a);
System.out.println("instance variable value is initialized for every instance "
  + instanceVariableTest3.a);
//We can not access the variable "e" out side the method because it is 
  accessable only inside the addNumbers() method.So following line causes 
  errors. So please comment it
//System.out.println(instanceVariableTest1.e +
  StaticInstanceVariableTest.e);
instanceVariableTest1.addNumbers(2, 4);
}

}
************************************

No comments:

Post a Comment