« Previous Page Next Page »
    

Introduction to Variable Types in Java.

POSTED

AUTHOR: VARUN SINGH

A variable is a container which holds the value while the java program is executed. A variable is assigned with a datatype. Variable is a name of memory location. There are three types of variables in java: local, instance and static.

In order to use a variable in a program you to need to follow two steps:-

1. Variable Declaration

2. Variable Initialization


Java Variable Declaration:

To declare a variable, you must specify the data type & give the unique variable name.


java variable declaration

Java Variable Initialization:

To initialize a variable, you must assign a valid value to variable.


java variable initialization

• Following are valid examples of variable declaration and initialization in Java:-

int a, b, c;         // Declares three ints, a, b, and c.
int a = 20, b = 30;  // Example of initialization
byte B = 20;         // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char a = 'a';        // the char variable a is initialized with value 'a'

Types of variables:

In Java, there are three types of variables:

• Local Variables

• Instance Variables

• Static Variables




Example: Types of Variables in Java

class Student {
    int age = 20; //instance variable  
    static int marks = 50; //static variable  
    void displayRecord() {
        int total = 90; //local variable  
    }
}


Local Variables:

• Local Variables are a variable that are declared inside the body of a method.

• variable will be destroyed once it exits the method.

• Access modifiers cannot be used for local variables.

• Local Variable does't have default value.

• Initial value should be assigned before the first use.

Example:

Here, local variable age is defined inside displayRecord() method and its scope is limited to only this method.

public class Student {
   public void displayRecord() {
      int age = 0;
      age = age + 15;
      System.out.println("Student age is : " + age);
   }

   public static void main(String args[]) {
      Student s1 = new Student();
      s1.displayRecord();
   }
}

Output:

Student age is : 15


Instance Variables:

• Instance variables are declared in a class, but outside a method.

• Instance variables are defined without the static keyword.

• Instance variables are created when an object is created with keyword "new".

• Instance variables are destroyed when the object is destroyed.

• Instance variables can be declared in class level before or after use.

• Access modifiers can be given for instance variables.

Example:

public class Student {

   // this instance variable is visible for any child class.
   public String name;

   // rollno variable is visible in Student class only.
   private int rollno;

   // The name variable is assigned in the constructor.
   public Student (String stuName) {
      name = stuName;
   }

   // The rollno variable is assigned a value.
   public void setRoll(int stuRoll) {
      rollno = stuRoll;
   }

   // This method prints the student details.
   public void printStu() {
      System.out.println("Name  : " + name );
      System.out.println("Roll No : " + rollno);
   }

   public static void main(String args[]) {
      Student s1 = new Student("Varun");
      s1.setRoll(15);
      s1.printStu();
   }
}

Example:

Name  : Varun
Roll No :15

Static Variables

• static variables are declared with the static keyword in a class

• Static variables are initialized only once, at the start of the program execution.

• These variables should be initialized before the initialization of any instance variables.

• Static variables are stored in the static memory.

• Static variables are created when the program starts.

• Static variables are destroyed when the program stops.

• Static variables can be accessed with the class name ClassName.VariableName.

Example:

public class Student1 {

   // marks  variable is a private static variable
   private static int marks;

   // Subject is a constant
   public static final String SUBJECT = "Science";

   public static void main(String args[]) {
      marks = 650;
      System.out.println(SUBJECT +" " + "Marks:" + marks);
   }
}

Output:

Science Marks:650




The End:

Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section. We will be very happy to hear that.

If you like my tutorial, please follow us on facebook, Google + and Twitter. I need your support to continue.

« Previous Page Next Page »

Comments: