What is Variables and Types of Variables in java?

Variables and Types of Variables in java? 

Definition:- Variables are Identifiers that help to store the Data.

Variables and Types of Variables in java

Types of Variables in Java.:-

1. Global Variable
      • Static Variable/class Variable.
      • Non-static Variable / Instance variable.
2. Local Variable

types of Variables in Java

What is Global Variable:-

A Global variable is a variable which is declared directly within the class, outside the method or constructor.

  • If the Global variable is static, then it is called a class variable. 
  • If the Global variable is non-static, then it is called the Instance variable.
â—¾Example of Global Variable : 
1:  public class Student {  
2:     String name; // Global variable  
3:     double perc=87.64; // Global variable  
4:     static String institute="Spiders";//Global Var  
5:   void study() {  
6:      double noOfHours=5.5;// Local Variable  
7:  }  
8:  }  


Must Read:What should you need to know before learning java?


What is Instance Variable:-

  • A non-static Global variable is called Instance Variable.
  • Instance Variable is created in memory only when an object gets created.
  • The number of copies of each instance variable depends on the number of objects.
  • Instance Variable is stored inside an Object as a part of Heap memory. 

If Instance variable is not initialized at the time of declaration then it is initialized to a default value at the time of Object creation.

â—¾Example of Instant Variable :
1:  class Shop {  
2:      long contact;  
3:      String addr;  
4:   public static void main(String[] args)  
5:  {  
6:       Shop s1 = new Shop();  
7:       Shop s2 = new Shop();  
8:       s2.contact=455644464L;  
9:       s2.addr="Delhi";  
10:      Shop s3 = new Shop();  
11:      s3.contact=1543265563L;  
12:      s3.addr="New_Delhi";  
13:  }  
14:  }  

Instance Variable

One More for better understanding:-
1:  public class Tree {  
2:  String type="Potato";  
3:  double height;  
4:  boolean giveFruit=false;  
5:  }  

Must Read:- What is ClassLoading in java?


Scope of Instance Variable:

What is Instance Variable
  • An Instance Variable can be accessed throughout the class and also can be accessed outside the class.
  • An Instance Variable can be accessed within the same class by using this keyword.
  • Instance variable can be accessed outside class using Object reference
â—¾Example:-
1:  public class Car {  
2:       String model="Bmw";  
3:       void start() {  
4:       System.out.println(this.model);  
5:    }  
6:    void move() {  
7:       System.out.println(this.model);  
8:       }  
9:  }  
10: public class Driver {  
11:     void drive() {  
12:         Car c=new Car();  
13:         System.out.println(c.model);  
14:    }  
15:  }  

What is Static Variable in Java:- A static variable is common to all the instances (or objects) of the class because it is a class-level variable. In other words, you can say that only a single copy of the static variable is created and shared among all the instances of the class. Memory allocation for such variables only happens once when the class is loaded in the memory.

Syntax of Static variable:- 
 
1:     static keyword followed by data type, followed by a variable name.  
2:     static data_type variable_name;  

mentioned above that the static variables are shared among all the instances of the class, they are
useful when we need to do memory management. In some cases we want to have a common value
for all the instances like global variable then it is much better to declare them static as this can save
memory (because the only single copy is created for static variables).

â—¾Example:-
1:  public class Demo {  
2:      static int count=0;  
3:    public void increment()  
4:    {  
5:       count++;  
6:    }  
7:    public static void main(String args[])  
8:  {  
9:          Demo obj1=new Demo();  
10:         Demo obj2=new Demo();  
11:         obj1.increment();  
12:         obj2.increment();  
13:         System.out.println("Obj1: count is="+obj1.count);  
14:         System.out.println("Obj2: count is="+obj2.count);  
15:  }  
16:  }  

â—¾Output:-
 Obj1: count is=2  
 Obj2: count is=2  
Note:- As you can see in the above example that both the objects are sharing the same copy of a static variable that’s why they displayed the same value of count.

Static Variable can be accessed directly in a static method:-

â—¾Example:-
1:  public class Demo {  
2:      static int age;  
3:      static String name;  
4:     //This is a Static Method  
5:    static void disp(){  
6:       System.out.println("Age is: "+age);  
7:      System.out.println("Name is: "+name);  
8:  }  
9:    // This is also a static method  
10:    public static void main(String args[])  
11:  {  
12:         age = 20;  
13:         name = "Rose";  
14:         disp();  
15:  }  
16:  }  

â—¾Output:-
 Age is: 30  
 Name is: Rose  

Static variable initialization:-

  1. Static variables are initialized when class is loaded.
  2. Static variables are initialized before any object of that class is created.
  3. Static variables are initialized before any static method of the class executes.
  • Default values for static and non-static variables are the same.
  • primitive integers(long, short, etc): 0
  • primitive floating points(float, double): 0.0
  • boolean: false
  • object references: null

What is Local Variable In java:-
  • A local variable is a variable that is created within a method or constructor or block.
  • Local variables must be initialized before use. I .e.., Default initialization is not applicable for a local variable.
  • The scope of the local Variable is Limited.
  • Local variable cannot be accessed using Object reference or by using this keyword.

Post a Comment

0 Comments