Variables in Java

A variable is something which is associated with a value can be changed. For example: int i=10; Here variable name “i” which is associated with with value 10, int is a data type that represents that this variable can hold integer values.

Note: We will cover the data types in the next tutorial. In this tutorial, we will discuss about variables.

How to Declare a variable in Java

The syntax to declare the variable is: data_type variable_name = value; here value is optional because in java, you can declare the variable first and then later assign the value to it. int num; Similarly we can assign the values to the variables while declaring them: char ch = ‘A’; int number = 100; or we can do it like this: char ch; int number; … ch = ‘A’; number = 100;

Variables naming convention in java

  • Variables naming cannot contain white spaces, for example: int roll number = 10; is invalid because the variable name has space in it.
  • Variable name can begin with special characters such as $ and _, for example int $number.
  • As per the java coding standards the variable name should begin with a lower case letter, for example int number; For lengthy variables names that has more than one words do it like this: int rollNumber; int serialNumber; (start the second word with capital letter).
  • Variable names are case sensitive in Java.

Types of Variables in Java

There are three types of variables in Java.

  • Local Variable
  • Static (or class) variable
  • Instance variable

1) Local Variable

Local variables are declared inside within the method of the class i.e their scope is limited to that method in which it is declared, you can’t change their values and access them outside of the method.

Example:
public class VariableExamples{
    public void myMethod(){
        //local variable
        string myVar= "Inside Method...";
        System.out.println(myVar);
        }
}        
Output:

Inside Method…

Note: If I hadn’t declared the instance variable and only declared the local variable inside method then the statement System.out.println(obj.myVar); would have thrown compilation error. As you cannot change and access local variables outside the method.

2) Static (or class) variable

Any variable when declared with the keyword “static” is known as static variable or class variable in JAVA. Static variable is used to fulfill the common properties of all objects. For example, If I create three objects of a class and access this static variable, it would be common for all, the changes made to the variable using one of the object would reflect when you access it through other objects.

Example:
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

3) Instance variable

Each instance(objects) of class has its own copy of instance variable. Unlike static variable, instance variables have their own separate copy of instance variable. We have changed the instance variable value using object obj2 in the following program and when we displayed the variable using all three objects, only the obj2 value got changed, others remain unchanged. This shows that they have their own copy of instance variable.

Example:
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

Related Post

E-Mail Format Formal Email / Letter Format A formal email is in general sent to someone you don’t know well or to someone who’s in authority. Examples of someone w...
Getting started with C Getting started with C : If we want to communicate with our computer it is very much needed that our computer understands what we want to say to it. ...
Custom View by use of xib in swift Custom View by use of xib in swift We can create the custom and common view by the use of xib in swift 3.1 and XCode 8.3.2 . Custom views are common ...
New features in XCode 10 Supported Configurations Xcode 10 beta requires a Mac running macOS 10.13.4 or later Xcode 10 beta includes SDKs for iOS 12, watchOS 5, macOS ...
C Language – Introduction Why C? There are three important aspects of any language so as C has : The way it stores data, The way it operates on this data and How th...
New Features XCode 12 – IDE Supported Configurations Xcode 12 beta includes SDKs for iOS 14 iPadOS 14 tvOS 14 watchOS 7 macOS 11 The Xcode 12 beta release supports o...
Author

Write A Comment