Friday, July 20, 2012

Java variable types

 There are 3 types of variable in java
  1. Local variable
  2. Instance variable or member variable
  3. Class variable or static variable
Local variable :
  • Local variables are declared inside methods, constructors, or blocks.
  • Local variable accessed from it's block only.
  • Local variables uses process stack memory not heap space memory.
  • Local variables have to initialized they don't have any default value.
Example code :

package com.fastlearned;

public class TestVariable {
 {
           // example of a local variable inside the block
  int block_v = 20;
  System.out.println("Inside a block : "+block_v);
 }
 TestVariable() {
  // example of a local variable inside the constructor
  int constructor_v = 30;
  System.out.println("Inside a constructor : "+constructor_v);
 }
 /*
  * Example of a local method
  */
 public void localMethod() {
  // example of a local variable inside a method
  String local_v = "Great Example in fastlearned";
      System.out.println("Inside a method : "+local_v);
 }
 /**
  * @param args
  */
 public static void main(String[] args) {

  TestVariable testVariable = new TestVariable();
  testVariable.localMethod();
 }

}

output :
Inside a block : 20
Inside a constructor : 30
Inside a method : Great Example in fastlearned 


Instance Variable or member variable
  • Instance variables are declared inside a class, but outside of a method, constructor or block.
  • Space for an instance variable is allocated in JVM Heap Space.
  • Instance variable has a default value.
  • Instance variable can be accesses from anywhere inside the class except static block or method.
  • Instance variables are accessed outside of a class also according to their access modifier.
Code Example :

package com.fastlearned;

  public class TestVariable {
 
 private int m_var = 10;
 private int m_var2 = 20;
 
 TestVariable() {
  // member or instance variable accessed from the constructor
  System.out.println("Inside a constructor : "+m_var);
 }
 /*
  * Example of a local method
  */
 public void localMethod() {
  // member or instance variable accessed inside a method
  
      System.out.println("Inside a method : "+m_var2);
 }
 /**
  * @param args
  */
 public static void main(String[] args) {

  TestVariable testVariable = new TestVariable();
  testVariable.localMethod();
 }

   }

Output code :
Inside a constructor : 10
Inside a method : 20

Class variable or static variable :
  • Varible those are declared inside a class using static keyword but outside block,constructor or method.
  • A single copy of these variable is used among multiple instances of the class.
  • Static variable are called by using their [class name].[variable name]

Code Example :

package com.fastlearned;

public class TestVariable {

 private static int s_var = 10;

 TestVariable() {
  // static or class variable accessed from the constructor

  System.out.println("Inside a constructor : " + s_var);
 }

 /*
  * Example of a local method
  */
 public void localMethod() {
  // member or instance variable accessed inside a method

  System.out.println("Inside a method : " + s_var);
 }

 /**
  * @param args
  */
 public static void main(String[] args) {

       TestVariable testVariable = new TestVariable();
       testVariable.localMethod();
       System.out.println("Out side the class " + TestVariable.s_var);

 }

}
output :
Inside a constructor : 10
Inside a method : 10
Out side the class 10

No comments:

Post a Comment