Variables in Java

Variable is a name of reversed memory area of any values. Variable’s value can be change during execution of program.Variable is for representing data of a specific type. In other words java variable store information which is needed to execute your program. It can be name , user Id etc. and as per
logic you can change it.

Table Of Content

Variable declaration in java

To use a variable we need declare variable first . You declare it by telling the compiler its name as well as what type of data it can hold. The variable declaration tells the compiler to allocate appropriate memory space for variable based on its data type. To declare the variable in Java, we can use following syntax as

datatype variableName;

Here, datatype is which type value store by variable which can any like: int, float etc. and variableName can be any like salary , name etc. Now we see How do you create a variable in java below example

public class JavaVaraible{
    public static void main(String[] args) {
        int salary ;
    }
}
Here , we have declare variable salary of int type . It means salary variable can hold int type value.

Initialize variables in java

Now we see how we can initialize a variable in java.  We will declare and initialize a variable with value 10 as following program

public class JavaVaraible{
    public static void main(String[] args) {
        int salary =10;
        salay= salary+ 1000;
        System.out.println(" salary ="+salary);   
    }
}

In this program we store salary in a variable 1000 and it will become 2000 when we added 1000 to it.We can print variable in java (read variable in java ) by using it’s name here .In details read a variable we will see given below examples.

Types of variables in java

There are three type variables in Java
1. Local variable
2. Instance variable
3. Static variable

1. Local Variable

Local Variable in java have following properties

  • Variable declare inside method is called local variable.
  • These variable only access inside the specific method/block/constructor.
  • Access modifiers cannot be used for local variables.

We will create local variable as following example

publicclassLocalVariable {


publicstaticvoidmain(String[] args) {
        LocalVariable localVariable = newLocalVariable();
       localVariable.display();
 }
     publicvoiddisplay(){
             inti = 10;// Local Varaible
             System.out.println(" i ="+i);
       }
}
Output:
i =10

2. Instance Variable :

Instance variable in java have following properties

  • Variable declare inside the class but outside method is called instance variable.
  • Instance variable belongs to object , not to class.
  • Access modifiers can be used for instance variables.

Now we see examples of instance variable in java.

publicclassInstanceVariable {
   int i=20;
   public static void main(String[] args) {
           InstanceVariableinstanceVariable = newInstanceVariable();
            System.out.println(" instance variable ="+ instanceVariable.i);
         }
    }
Output :
instance variable = 20

3. Static Variable

Static variable have following properties in java

  • Variable declare inside class with static keyword is called static variable.
  • Static variable can not local.
  • Static variable belongs to class ,not to object.
  • Access modifiers can be used for static variables.
  • Static variable access as ClassName.StaticVariableName.

Let’s see this by code

publicclassStaticVariable {

     static int i=100;

   public static void main(String[] args) {
            System.out.println("price of bag is ="+ i);
    }
}
Output :
price of bag is =100

Java Variable Naming Conventions

There are a few rules for naming conventions of variables as following

  • Java variable names are case sensitive. The variable name salary is not the same as Salary or SALARY.
  • Java Variable names must begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
  • After the first character, identifiers can have any combination of characters.
  • A reserved keyword cannot be used as variable.
  • Examples of legal identifiers: name ,$salary, _MAX, __5_value.
  • Examples of illegal identifiers: 123abc, -salary.

We can follow Variable naming convention as All instance, static and method parameter variable names should be in camel case notation. They should be short and enough to describe their purpose In Program. Temporary variables can be a single character e.g. the counter in the loops.

References :

A Guide To Java

Java Doc – Variable

 

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

98 − = 94