print
Advertisment
Advertisment

Variables in java

variables are the name of the memory location that holds data values. This data can be any type of information. we need to assign every variable with a data type .variable value can be changed. This data can be any kind of information from text to numbers, temporary results of operations, etc.

Example

Student.java
public class Student {
    String name = "Ramakant rawat";
    int rollNo = 1024;
    String colegeCode ="C121J312";
    Student st = new Student();
    public void method1(){
        Stirng localVariable = "I am a local variable";
    }
    public void method2(String parametersVariable){

    }
}

Based on the behavior and position of the declaration, there are three types of variable

  1. Local variables
  2. Instance variables
  3. Static variables

Based on the type of value represented by a variable there are two types of variable

  1. Primitive variables
  2. Reference variables

Based on the behavior and position of the declaration

Local variable

A variable declared inside the body of a method is called a local variable. We can use local variable only inside the method in which it is declared.

Example

Student.java
public class Student{
    public static void main(String[] args) {
        int i = 178; //here i is local variable
        System.out.println(i);
    }
}

Output :

   
    
    178

Process finished with exit code 0

    
Advertisment

Static Variables(class variables)

Static variables are declared using static keywords. If any variable’s value is not changed from object to object then we need to declare that variable as a static variable because these are declared only one time and we can use these anywhere in that class. These are created at the time of class loading and destroyed at the time of class unloading .these are declared inside the class but outside the method.

  • Any variable that is created using the static keyword is called a static variable.
  • The static variable is of class label. That's why it is also called a class variable.
  • It is also stored in the heap memory area.
  • Static variables make exactly one copy of the variable in existence, regardless of how many times the class has been instantiated. In our code, & for all objects. which takes up less space in memory

Example

Student.java
class Student {
    private static String name = "Ramakant rawat";
    public static void main(String[] args) {
        Student obj1 = new Student();
        Student obj2 = new Student();

        System.out.println(obj1.name);      
        System.out.println(obj2.name);      
    } 
}

Output :

   
    
    Ramakant rawat
    Ramakant rawat
    

Note: in the above program only one memory is allocated for the variable `name` because it's static and all objects can access this

Instance variable

These are declared inside the class but outside the body of the method or block or constructor. we can access these directly from the instance area but not directly from the static area. But we can access these from the static area by using object reference

Example

ExampleOfVariables.java
public class ExampleOfVariables{
    int p = 178; //instance variable
    static int t = 278;  //static variable
    public static void main(String[] args) {
        int r = 100; //local variable
        System.out.println(r);
        System.out.println(t);
        System.out.println(p);/* it will throw error because we can not access instance
        variable from an static area directly*/
    }
}

Output :

   
    
   java: non-static variable p cannot be referenced from a static context
    
ExampleOfVariable.java
public class ExampleOfVariable{
    int p = 178; //instance variable
    static int t = 278;  //static variable
    public static void main(String[] args) {
        int r = 100; //local variable
        System.out.println(r);
        System.out.println(t);

    }
}

Output :

   
    
   100
   278
    

Based on the type of value represented by a variable

Primitive variables

The variables that are used to store primitive data types are called primitive variables.

Example

ExampleOfPrimitiveVariables.java
public class ExampleOfPrimitiveVariables{
    public static void main(String[] args) {
        int i = 274;
        short s = 47;
        float f =34.3247f;
        System.out.println(i);
        System.out.println(s);
        System.out.println(f);
    }
}

Output :

   
    
    274
    47
    34.3247

    Process finished with exit code 0

    
Advertisment

Reference variables

The variables that are used to store objects are called reference variables. Classes, Arrays, Interfaces, etc are reference types and reference variable holds the values of these reference types. With the help of reference variables, we can access object members

Example

ExampleOfReferenceVariables.java
public class ExampleOfReferenceVariables{
    int i = 90;
    int Display(){
        System.out.println("i ="+i);
        return 0;

    }
}
class Example{
    public static void main(String[] args) {
        ExampleOfReferenceVariables M1 = new ExampleOfReferenceVariables(); //here ExampleOfReferenceVariables is reference variable 
        System.out.println(M1.Display());
    }
}

Output :

   
    
    i =90
    0

    Process finished with exit code 0

    
Advertisment
Advertisment
arrow_upward