Java Variables

 

Java Variables

Variables are containers that store information that can be manipulated and referenced later by the programmer within the code. Java variables have a data type associated with them which can tell us about the size and layout of the variable’s memory.


Syntax:

datatype variable = value

 

There are three types of variables in java:


  • Local variable
  • Instance variable
  • Class/Static variable

 

A. Local Variables:

A variable that is declared inside the body of the method or constructor is called a local variable. It is called so because the extent of a local variable lies only within the method or constructor within which it is created and other methods in the class cannot access it.

Inside the method body, local variable is declared using the static keyword.


Example:

public class variableType {
    public void localVariable() {
        String name = "Ben";
        int marks = 95;
        System.out.println(name + " Scored " + marks + "%.");
    }

    public static void main(String[] args) {
        variableType vt = new variableType();
        vt.localVariable();
    }
}

 

Output:

Ben Scored 95%.

 

If it is accessed outside the method or constructor within which it is creaed, then it give an error.


Example:

public class variableType {
    public void localVariable() {
        String name = "Ben";
        int marks = 95;
    }
    public void notLocalVariable() {
        System.out.println(name + " Scored " + marks + "%.");
    }

    public static void main(String[] args) {
        variableType vt = new variableType();
        vt.notLocalVariable();
    }
}


Output:

name cannot be resolved to a variable
marks cannot be resolved to a variable

 

B. Instance Variables:

An instance variable is declared inside the class but outside a method or a constructor. It is similar to a static variable except that it is declared without using the keyword static. These variables are accessible by all methods or constructors that are inside the class.


Example:


public class variableType {

    public String name = "Ben";
    public int marks = 95;

    public void instanceVariable() {
        System.out.println(name + " Scored " + marks + "%.");
    }

    public static void main(String[] args) {
        variableType vt = new variableType();
        vt.instanceVariable();
    }
}

 

Output:

Ben Scored 95%.

 

C. Class/Static Variables:

An static variable is declared inside the class but outside a method or a constructor. It is similar to a instance variable except that it is declared using the keyword static. These variables are accessible by all methods or constructors that are inside the class.


Example:

public class variableType {

    public static String name;
    public static int marks;

    public static void main(String[] args) {
        name = "Ben";
        marks = 95;
        System.out.println(name + " Scored " + marks + "%.");
    }
}

 

Output:

Ben Scored 95%.


If variable is not declared static the it gives an error.


Example:

public class variableType {
    public String name;
    public int marks;

    public static void main(String[] args) {
        name = "Ben";
        marks = 95;
        System.out.println(name + " Scored " + marks + "%.");
    }
}

 

Output:

Cannot make a static refrence to a non-static field