Variables in Java

Welcome back to the Java tutorials. In the last session we focused on different types of data types available in Java. We briefly discussed both the primitive data types as well as non-primitive data types.

Coming back to the current tutorial, today we would discuss about the various types of variables that a user can create in Java. We would talk about the local variables, class variables and instance variables. We would also look at their practical implementations.

Our classes are usually made-up data members and the methods to manipulate them. Variables are used as identifiers to store the data which can be exploited and manipulated using methods. As we discussed the various data types in our last tutorial, so each of the variable belongs to a specific data type and that is how we get to know the memory allocated, size, type and the bunch of the operations that can be performed on the data that can be stored in the variable.

Examples:

int a; // Declaration of  a single integer variable
int a, b, c; // Single line declaration of three integer variables
int a = 10; // Value assignment or initialization
int a = 10, b = 20, c = 30; // Initializing multiple values
int a=b; // Initializing a variable using another variable
char a = ‘a’; // character initialization
float value = 7.3; // float initialization
double value = 6.567; // double initialization

To be able to perform any operation on the data, we must declare and initialize them using variables. Hence from the above examples, we can come up with the variable declaration and initialization syntax.

Syntax:

data type variable_name = value;

Java provides three types of variables:

  • Local Variables
  • Class Variables
  • Instance Variables

Let us quickly have a look at each one of them.

Local Variables

1

Note: Compiler doesn’t provide any default value to the local variables. Thus, the user is essentially required to initialize the local variables prior to their usage.

Example

public class FirstClass {
// method to access local variable
    public void sum() // 
    {
        // Initialization of local variables
        int a = 10;        
int b = 17;
        int c = 0;        
c = a+b; // operation
        System.out.println("The value of sum is: "+c);
    }
    public static void main(String[] args) {
        FirstClass obj = new FirstClass();                
        obj.sum();        
    }
}

Output:

The value of sum is: 27

Class Variables

2

Default Values for Class Variables:

Untitled

Example:

public class ClassVariable{
   // declaration of a class variable
   private static int rollNo;

   // name is created as a constant value
   public static final String name = "Shruti";

   public static void main(String args[]){
      rollNo = 58;
      System.out.println(name+"'s roll no is: "+rollNo);
   }
}

Output:

Shruti’s roll no is: 58

Instance Variables

3

Example:

public class InstanceVariable{
   // Instance Variable that can be accessed by child classes also
   public String name;
   
   // Instance Variable that can only be accessed by InstanceVariable class
   private int rollno;
   
   // Parameterized Constructor
   public InstanceVariable (String Name){
      name = Name;
   }

   public void setRollNo(int RollNo){
       rollno = RollNo;
       System.out.println("name is: "+name);
       System.out.println("Rollno is: " + rollno);
   }

   public static void main(String args[]){
      InstanceVariable obj = new InstanceVariable("Shruti");
      obj.setRollNo(59);
   }
}

Output:

name is: Shruti

Rollno is: 59

With this, we come to an end of this tutorial. Stay tuned for the upcoming tutorials. We will keep posting more of the useful stuff on Java. Do share your reviews, comment and queries.

Shruti Shrivastava

Hey there, I'm Shruti Shrivastava. A Quality Engineer with an enthusiasm towards technical and non-technical writing. I've been into this profession for 3+ years now and thought of sharing my experiences and thus the fortunate turn of events has lead me to join Abode QA. When i am not glued to my system, i could be found reading novels, writing poems, enjoying with friends. Contact Me @ : shruti.shrivastava.in@gmail.com http://www.linkedin.com/profile/view?id=74986132&trk=nav_responsive_tab_profile

You may also like...

Leave a Reply

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