Java

What Variables Does Java Have?

A variable is a name that has a movable value attached to it. For instance, the data type int indicates that the variable may contain integer values when I write int i=10, where I is a variable whose value is 10. The data types will be discussed in the upcoming tutorial. We’ll talk about variables in this lesson.

How to Declare a variable in Java

Use the following syntax to declare a variable:

data_type variable_name = value;

Value is unnecessary in this case since declaring a variable in Java allows you to later assign a value to it.

As an illustration, the data type int is used here and num is a variable. Don’t worry too much about the data type; just know that it permits this num variable to contain integer values until we explore it in the following session. Although you may learn about data types here, I advise you to finish this guide before moving on to the next.

int num;

In a same manner, we may assign values to the variables as we declare them, as in:

char ch = 'A';
int number = 100;

or we might do it this way:

char ch;
int number;
...
ch = 'A';
number  = 100;

Variables naming convention in java

1) White spaces are not permitted in variable names. For instance, the statement “int number = 100” is incorrect because it has a space in the variable name.
2) Special characters like $ and can be used to start the name of a variable.
3) In accordance with Java coding guidelines, the variable name should be preceded by a lowercase letter, such as int number; Do it this way for long variable names with several words: smallNumber; bigNumber; int (start the second word with capital letter).
4) In Java, variable names are case-sensitive.

Types of Variables in Java

In Java, there are three distinct types of variables.

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

Static (or class) Variable

Due to their association with the class and shared characteristics across all instances of the class, static variables are sometimes referred to as class variables. The modifications made to the static variable using one of the instances would reflect when you accessed it using other objects, for instance, if I created three objects of the same class.

Example of static variable

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);

Output:

class or static variable
class or static variable
class or static variable
Changed Text
Changed Text
Changed Text

As you can see, regardless of the instance through which it is accessed, all three statements produced the identical output. Because to this, we may access static variables without requiring objects, as seen below:

System.out.println(myClassVar);

Instance variable

A copy of the instance variable exists for each instance (object) of the class. In contrast to static variables, instance variables have a unique copy of the instance variable for themselves. In the following program, we modified the instance variable value using object obj2, and when we showed the variable using all three objects, only the obj2 value changed, the other values remained the same. This demonstrates that they have a unique instance variable.

Example of Instance variable

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);
   }
}

Output:

instance variable
instance variable
instance variable
instance variable
Changed Text
instance variable

Local Variable

These variables are declared within a class method. Their scope is restricted to the method, therefore you are unable to access or modify them outside of the method.

To illustrate the range of local variables, I have defined an instance variable in this example with the same name as a local variable.

Example of Local variable

public class VariableExample {
   // instance variable
   public String myVar="instance variable";
    
   public void myMethod(){
    	// local variable
    	String myVar = "Inside Method";
    	System.out.println(myVar);
   }
   public static void main(String args[]){
      // Creating object
      VariableExample obj = new VariableExample();
    	
      /* We are calling the method, that changes the 
       * value of myVar. We are displaying myVar again after 
       * the method call, to demonstrate that the local 
       * variable scope is limited to the method itself.
       */
      System.out.println("Calling Method");
      obj.myMethod();
      System.out.println(obj.myVar);
   }
}

Output:

Calling Method
Inside Method
instance variable

 

Related Articles

Leave a Reply

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

Back to top button