Types of java variables

Java variables are very straightforward, just they are variables.

Yes, just they are variable but we have to understand when the memory allocated and how can we access those.

I have seen some people who can write the code without knowing all this.

Seriously!, is that possible ?


   - Yes, it's possible when you follow the someone code blindly but we can't give efficient solution to our client.
   - If any serious memory issue comes you will never have an idea to fix it.

So, it is very important to understand the variable memory allocation.

Here I'm concentrating only on what variable in what memory and how we can access.


 Types of variable.

Instance variable:
 A variable which is defined as a member of class.

Instance variable memory  allocated whenever an object is created.
So, we must access with object or object reference.

All instance variables are stored in heap memory.

public class MleClass{
 int valueOne=10;  // Instance variable
 String stringValue= "Hello";  // Instance variable

// Instance method.
  void show(){

 }

 public static void main(String args[]){
  // Must access with object or object reference.

    System.out.println(new MleClass().valueOne);// With object 
    MleClass obj = new MleClass();
    System.out.println(obj.stringValue);// With object reference
      System.out.println(obj.show());
 }
}

Class variable/ Static variable:
 A variable which is defined as a static members of a class.

For Class/Static variables memory will allocate whenever class is loaded.
  So, we can access with class name(If it is in same class we can access directly).

All class variables are stored in method area.

class MleClassTwo{
  static String stringValue= "Hello";  // static variable
}

 public class MleClassOne{
 static int value=10;  // static variable
 public static void main(String args[]){
    System.out.println(value);// Direct access
    MleClassTwo mleTwoObj = new MleClassTwo();
  System.out.println(mleTwoObj.stringValue);// Still we can access but it is not best practice.
    System.out.println(MleClassTwo.stringValue);// With class name.
}
} 
 
Local variable:
 A variable  which is defined inside a method.

Memory will allocate whenever method is called.

All local variables are stored in  Stack area.
And local variable must be initialized.

class MleClass{
 public static void main(String args[]){
   int value =10;
    System.out.println(value);.
 }
}


Note: We can't defined a local variable with static.
          Why?
     
        As I explained for static/class variables memory allocates whenever class is loaded.

 Assume static members declared inside method like this.

This is not valid
class A{

// Instance members
 void show(){
 static int value= 10;
  
 }
 public static void main(String args[]){
  
 }
}

So, if I write a static variable inside a method what will happen?
  JVM look for the static members as soon as class loaded  but in above snippet instance method having the static member.

To execute the instance member we need class object.
So, JVM can't create object with out our permission.
That's what it's not possible.

No comments:

Post a Comment