Monday 30 May 2016

Variables in Java


In this blog post,we will learn about variables supported by java programming language.There are three kinds of variables in Java language.

1-Local variables

2-Instance variables

3-Class/static variables

Before discussing the following variable we will learn about what is a mean of variable in java

Variable:
"A variable provides a storage location to stored data in memory.The variable have three things"
1-Name
2-Data Type
3-Size
we give demo example to declare variable in java.
int a=56;                           // Variable declaration
In the above variable declaration
a =variable name
int= data type
size =32 -bit."

1-Local variables
"Local variables are declared inside methods,constructors,or blocks.Access modifiers cannot be used for local variables.Local variable will be destroyed after completing the method,constructor or block.Local variables are available only within the declared method, constructor or blocks."

Demo Example:

public class Employee{
   public void print(){
      String name=Ramesh;
   
      System.out.println("The employee name " + name);
   }
 
   public static void main(String args[]){
      Employee emp = new Employee();
      emp.print();
   }
}


2-Instance variables:

Instance variables are declared inside class, but outside a method,constructor or any block.When object is created  JVM allocates memory in heap for object storage.Instance variables hold values that must be referenced by more than one method or constructors.An object's state that must be present throughout the class.Instance variables can be declared in class level before or after use.Access modifiers can be given for instance variables.


Instance variables can be accessed directly by calling the variable name inside the class. However within static methods and different class should be called using the fully qualified name ObjectReference.VariableName.

Demo Example:


import java.io.*;

public class Vechile{
 
   public String name;

   private int speed;
   public Vechile (String name){
      name =name;
   }

   public void speeed(int speed){
      speed =speed;
   }
 
   public void printVechileSpeed(){
      System.out.println("name  : " + name );
      System.out.println("speed :" + speed);
   }

   public static void main(String args[]){
      Vechile vc1 = new Vechile("Car");
      vc1.speed(100);
      vc1.printVechileSpeed();
   }
}


3-Class/static/Global variables:

Class variables also known as static variables are declared with the static keyword in a class,but outside a method, constructor or a block.Static variables are stored in static memory.

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 created when the program starts and destroyed when the program stops.Static variables can be accessed by calling with the class name ClassName.VariableName.

Demo Example:
import java.io.*;

public class A{
 
   private static String name;

   public static void main(String args[]){
   name=xyz;
  System.out.println("Hello "+name);


   }
}



No comments:

Post a Comment