Java Variable

<<  < Previous Lesson < Java Tutorial > Next Lesson >  >>


 A variable is a container that hold value which is used in a java program. Each variable must be declared to use a data type. For instance, a variable could be declared to use one of the eight primitive data types. Such as byte, short, int, long, float, double, char and boolean. Every variable must be given an initial value before it can be used.
int a = 10;
 The variable "a" is declared to be an int data type and initialized to a value of 10.

Fundamental form of variable declaration is:
data type variable [ = value][, variable [= value] ...] ;
Here data type is one of Java datatype and variable is name of the variable. To declare more than one variable of the specific type we can use a comma to separate all.

There are example of variable declaration and initialization:
/* Here we declare and initialize int type variable */

int x = 3, y = 5, z = 7; 
or
int x = 3;
int y = 5;
int z = 7;
/* Here we declare and initialize byte type variable  */

byte X = 1; 
/* Here we declare double type variable and assign PI value */

double pi = 3.14159; 
/*Here we declare character type variable and initialize */

char x = 'x';  
There are three types of variable in Java:
>> Local variable
>> Instance variable
>> Class variable or static variable

>> Local variable
A local variable in Java is a variable that is declared within the body of a method. Then we can use the variable only within that method. Other methods in the class are not even aware that the variable exists.

Here is a program that uses local variable:
public class HelloWorldApp
{
    public static void main(String[] args)
    {
        String helloMessage;
        helloMessage = "Hello, World!";
        System.out.println(helloMessage);
    }
}
The local variable is declare in a method, constructor and block. Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block. Access modifiers cannot be used for local variables. Local variables are visible only within the declared method, constructor or block. Local variable are implemented at stack level internally. There is no default value for local variable so local variable should be declared and initialize initial value before use it primarily.

For instance age is a local variable. It's defined inside tutAge() method and it's scope is limited to this method only.
public class Tree{
   public void tutAge(){
      int x = 0;
      x = x + 103;
      System.out.println("This is an apple tree age: " + x);
   }
 
   public static void main(String args[]){
      Tree tree = new Tree();
      tree.tutAge();
   }
}
Output:
This is an apple tree age: 103
>> Instance variable
In object oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable), for which each instantiated object of the class has a separate copy, or instance. An instance variable is similar to and contrasts with a class variable. An instance variable is not a Class variable. It is a type of class attribute (class property, field, or data member). The same dichotomy between instance and class members applies to methods ("member functions") as well; a class may have both instance methods and class methods. Each class variable and instance variable you invoke with the object lives in memory for the life of that object. Instance variables are properties an object knows about itself. All instances of an object have their own copies of instance variables, even if the value is the same from one object to another. One object instance can change values of its instance variables without affecting all other instances. Instance variables can be used by all methods of a class unless the method is declared as static.

or

Instance variables are any variables, without "static" field modifier, that are defined within the class body and outside any class's methods body.

Instance variables are in scope as long as their enclosing object is in scope. An instance variable is a part of the object that contains it and cannot live independently of it.

All object instances have their own copies of instance variables. One object instance can change values of its instance variables without affecting all other instances.

Instance variables can be used by all methods of a class unless the methods are marked with "static" modifier. You access instance variables directly from their containing object instances.

Example of an instance variable is:
class Taxes
{
  int count;
  /*...*/
}

For instance, Program:
import java.io.*;

public class Employee{
   // this instance variable is visible for any child class.
   public String name;
 
   // salary  variable is visible in Employee class only.
   private double salary;
 
   // The name variable is assigned in the constructor.
   public Employee (String empName){
      name = empName;
   }

   // The salary variable is assigned a value.
   public void setSalary(double empSal){
      salary = empSal;
   }
 
   // This method prints the employee details.
   public void printEmp(){
      System.out.println("Name is : " + name );
      System.out.println("Salary is :" + salary);
   }

   public static void main(String args[]){
      Employee empOne = new Employee("AIA");
      empOne.setSalary(55000);
      empOne.printEmp();
   }
}
Output:
Name is : AIA
Salary is : 55000.0
>> Class variable or static variable
In Java Object Oriented Programming Language class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. There would only be one copy of each class variable per class, regardless of how many objects are created from it. Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final and static. Constant variables never change from their initial value. Static variables are stored in static memory. It is rare to use static variables other than declared final and used as either public or private constants. Static variables are created when the program starts and destroyed when the program stops. Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class. Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally values can be assigned in special static initializer blocks. Static variables can be accessed by calling with the class name . ClassName.VariableName. When declaring class variables as public static final, then variables names (constants) are all in upper case. If the static variables are not public and final the naming syntax is the same as instance and local variables.

* In Java (and in OOP in general) the objects have two kind of fields(variable).

* Instance variables(or object variable) are fields that belongs to a particular instance of an object.

* Static variables (or class variable) are common to all the instance of the same class.)

Example of a class variable is:
class Taxes
{
  static int count;
  /*...*/
}

  <<  < Previous Lesson < Java Tutorial > Next Lesson >  >>

Regularly visit this site to get new information about Java Programming Language. Today, it is a robust and most powerful programming language in the Universe.