/ Java

Variables in Java

Variable is the reserved memory location in the main memory (RAM). It is made up of “vary + able” that means the value can be changed. The actual value in the memory location changes during the execution of the program.

int number=50;  //number is the variable

Java Variables

A variable is like a container that holds the value while Java program is executed. It is also assigned with a datatype. In the example above int is a datatype. Furthermore, there are three types of variables in Java: local, instance and static.

It assigns the value with it which can be changed. For example, above, variable number is associated with 50, int is a datatype that denotes that variable can hold the integer values. There are two types of Datatypes viz. primitive and non-primitive.

Declaration of variables in Java

The general syntax of variable declaration in Java is

data_type variable_name = value;

e.g int myVar = 150;

 

Though we have assigned value in a variable it is optional in Java. You can assign a value later in the program.

We can assign the value as below:

char ch = ‘D’;

int number = 99;

or we can do it like this:

char ch;

int number;

ch = ‘D’;

number = 99;

Naming conventions of variables in Java

Though the name can be chosen arbitrarily as a variable name, there are certain rules that bind the name of a variable.

1) The variable name doesnot allow white spaces. E.g. int my Var =10; is invalid because the variable name has space.

2) Initial letter of variables uses special characters like $ and _.

3) The general convention of a variable is that they start with the lower case letter. E.g. int hello; for long words which has more than one words, like int nayaNumber, nayaThulo, etc. (make second-word capital).

4) The name of the variables is case sensitive in Java. i.e., int hello and int Hello are different from each other.

5) At last, it cannot use the reserved keywords like class, public, etc.

Types of Variables in Java with example

Based on the usage, Java has three types.  They are:

  1. Static Variable
  2. Instance Variable
  3. Local Variable

The different types of variable in java with example is illustrated as follows:

class Hello{

public static void main(String args[])

{

static int myVar =20; //static variable

int myVary =30;  //instance variable



public void myMethod()

{

int myStaticVar =40;

}

}

}

 

  1. Static Variable

The static keyword is the first type used to declare the Static Variables. They are also known as class variables as they are associated with the class and can be used by every instance of the class. When class is loaded in the memory, memory is allocated for the variable which is then shared by each instance of the class.

For example, if we created three instances of the class and use the static variable than they might get the same value stored in the common memory location. Even if some object among three changed the value, the change would reflect in all of them. So, this illustrates an example of static variable types in java.

Program of static variable

class StaticVarDemo {

static String myClassVar="I'm initial static variable";

public static void main(String args[]){

StaticVarDemo obj1 = new StaticVarDemo();

StaticVarDemo obj2 = new StaticVarDemo();

StaticVarDemo obj3 = new StaticVarDemo();



//All three will display "I'm initial static variable"

System.out.println(obj1.myClassVar);

System.out.println(obj2.myClassVar);

System.out.println(obj3.myClassVar);



//changing the value of a static variable using obj2

obj2.myClassVar = "Hello World";



//All three will display "Hello World"

System.out.println(obj1.myClassVar);

System.out.println(obj2.myClassVar);

System.out.println(obj3.myClassVar);

}

}

 

Output:

I’m initial static variable

I’m initial static variable

I’m initial static variable

Hello World

Hello World

Hello World

In the example above, all the statements provide the same output irrespective of their instance calling. So, we can access the static variable even without using objects like

System.out.println(myClassVar);

But it doesn’t work for instance and local variables as the values are associated with the class.

2) Instance Variable

The variable that are not inside any method but inside the class is known as instance variable. So, they don’t have a static keyword. They are another type which is associated with each instance of the class. So they have a different memory location for each of them. The value changed by one instance doesn’t affect other instance. So, this illustrates an example of Instance type of variable in java.

Program of Instance variable

class InstanceVarDemo {

String myInstanceVar="I'm Instance Variable";

public static void main(String args[]){

InstanceVarDemo obj1 = new InstanceVarDemo();

InstanceVarDemo obj2 = new InstanceVarDemo();

InstanceVarDemo obj3 = new InstanceVarDemo();



//First call to instance variable

System.out.println(obj.myInstanceVar);

System.out.println(obj2.myInstanceVar);

System.out.println(obj3.myInstanceVar);



//Changing the value of instance variable using obj2

obj2.myInstanceVar = "Object 2 Changed Text";



//Second call o instance variable

System.out.println(obj1.myInstanceVar);

System.out.println(obj2.myInstanceVar);

System.out.println(obj3.myInstanceVar);

}

}

 

Output:

I’m Instance Variable

I’m Instance Variable

I’m Instance Variable

I’m Instance Variable

Object 2 Changed Text

I’m Instance Variable

3) Local Variable

Local variable is declared certainly inside the methods. So, as the name suggests their scope is local to the method scope. It means you can only use the variable inside the method. One local variable of one method is not identified by the other local variable of another method.

It cannot have the static keyword.

Program of Local variable

In this program below, there is the instance variable with the same name as, local variable to demonstrate the scope of a local variable. So, this is an example of local type of variable in java.

class LocalVarDemo {

// instance variable with name myVar

public String myVar="I'm instance variable";



public void myMethod(){

// local variable with same name myVar as instance variable

String myVar = "I'm Inside Method";

System.out.println(myVar);

}



public static void main(String args[]){

// Creating object



//Calling method

System.out.println("Calling Method");

obj.myMethod();



//calling instance variable

System.out.println(obj.myVar);

}

}

 

Output:

Calling Method

I’m Inside Method

I’m instance variable

You can see that the value myVar inside myMethod and object itself produce different output despite being the same name. This is because the local scope output is produced by variable inside myMethod and instance output is produced by Instance variables.

 

Download this article

Dipendra Laxmi Bahadur Chand Thakuri
CEO @ Vine Software Innovation Company (Pvt.) Ltd. (VSIC)
View More

Comments:

2 comment(s)!
Programs to know Variables in Java Clearly - Saral Notes
[…] the previous article names Variables in Java, I have clearly described the scope and usage of various variables in Java, The use of local, […]
Data Types in Java - Saral Notes
[…] Variables in Java […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

back to top button
error: Please be respectful of copyright. Unauthorized use is prohibited.
%d bloggers like this: