A Java Variables, is a memory area that is used to store a value, the variable contains values ​​that can be changed at the time of program execution. For example int i = 10, here the variable name is i which is associated with the value 10, int is a data type indicating that this variable can store the integer value.

How to Declare Variable in Java

Use the following syntax to declare the variable
data_type variable_name = value;
Here the value is optional, if you want, you can declare the variable first and then assign the value to the variable later.

for example

int num;
Here num is a variable and int is a data_type, here int data type allows to store integer value for num variable.

Similarly, while declare the variable, one can also assign the value to the variable, something like this
int ch = ‘A’;
int number = 100;

And in another way the value can be assigned in the variable also, some like this –
char ch;
int number;
ch = ‘A’;
number = 100;

How to ensure the name of a variable in java

  1. White space cannot be used in variable names, for example int num ber = 100; It is invalid because it has space in variable name.
  2. The name of the variable can never start with some special characters like $ and _.
  3. According to java coding standards the name of the variable should always be written in lower case letters eg int number; If the variable name has more than one word, it can be written as – int bigNumber;
  4. In Java, the variable name is case sensitive.

Types of Variables

types of variable in java

There are 3 types of variables in java: –

  1. Local variable
  2. Static (class) variable
  3. Instance variable

Local variable

Such variables are declared within the class method. Their scope is limited to that method which means that the values ​​of variables declared inside the method can be changed outside the method and not these variables can be accessed outside the class.

In the example below, the local variable is explained –

Public class VariableExample{
	// instance variable
	String myVar=”instance variable”;
	public void myMethod (){
	// local variable
	String myVar = “instance method “;
	System.out.println(myVar);
}
Public class VariableExample {
	// instance variable
	String myVar=”instance variable”;
	public void myMethod (){
		// local variable
	String myVar = “instance method “;
	System.out.println(myVar);
}

Public void main(String args[]) {
	// creating object
	VariableExample obj = new VariableExample ();
	System.out.println(“calling method “);
	obj.myMethod();
	System.out.println(obj.myVar);
}

Output: –

calling method
inside method
Instance variable

Here, if the instance variable is not declared, only the local variable is declared, then System.out.println (obj.myVar); The statement contains a compilation error.

Static (or class) Variable

The static variable is also known as the class variable as it is associated with the class and is common to all instances of the class.

What are the differences between USB 2.0 and USB 3.0

Examples :-

Public class staticVarExample 
{
	Public static String myClassVar=”class or static variable”;
	Public static void main(String args[]) 
	{
		staticVarExample obj = new staticVarExample();
		staticVarExample obj2 = new staticVarExample();
		staticVarExample obj3 = new   staticVarExample();
		// All three will display “class or static variable “
		System.out.println(obj.myClassVar);
		System.out.println(obj2.myClassVar);
		System.out.println(obj3.myClassVar);
		//changing the value of static variable using obj2
		Obj2.myClassVar=”changed Text “;
		// all three will display changed text
		System.out.println(obj.myClassVar);
		System.out.println(obj2.myClassVar);
		System.out.println(obj3.myClassVar);
	}
}

Result

class or static variable
class or static variable
class or static variable

changed Text
changed Text
changed Text

As you can see in the example above, all three statements display the same output through which it is being accessed.

System.out.println(myClassVar);

Keep in mind that only the static variable can be accessed in this way. This example does not apply to instance and local variables.

Instance Variable

Unlike the static variables, the instance variable has its own different copy, we have changed the value of the instance variable using the object obj2 in the following program, the variable will be changed only when the object is displayed by all three objects and The values ​​of the remaining two objects will remain unchanged.

Public class InstanceVarExample 
{
	String myinstanceVar=”instance variable”;
	Public static void main(String args[]) 
	{
		InstanceVarExample obj = new InstanceVarExample ();
		InstanceVarExample obj2 = new InstanceVarExample ();
		InstanceVarExample obj3 = new InstanceVarExample ();
		System.out.println(obj.myInstanceVar);
		System.out.println(obj2.myInstanceVar);
		System.out.println(obj3.myInstanceVar);
		obj2.myInstanceVar="changed text";
		System.out.println(obj.myInstanceVar);
		System.out.println(obj2.myInstanceVar);
		System.out.println(obj3.myInstanceVar);
	}
}

Result

instance variable
instance variable
instance variable
instance variable
changed text
instance variable

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here