Friday, 22 July 2016

Types of variables in JAVA

Types of variables in JAVA:
=====================
There are 4 types variables are available in java

1. Non static variables

2. Static variables
3. Local variables
4. Object type variables


1.Non static variables: 
If the value of a variable is going to be change object to object  such type of variables we called as non-static  variables.
For every object a separate copy of instance variables will be created
Syntax:
Access specifier Datatype variable name = variable name;
When non static variables will be created?
Instance variables will be created at the time of object creation and destroyed at the time of object destruction
When do we declare a variable as non-static::
=======================================
Whenever the value of a variable is going to be change object to object then declare that variable as non-static
Access scope
=============:
 We can access non static variables throughout our program
Where do we declare non static variables?
After starting the class immediately we will declare non static variables directly
Default values:
Initialization of variables is optional. If we don’t perform initialization of variables JVM is going to assign the default values.
Ex:   public class TestDemo
{
Int x;
Public static void main (String [] a){
TestDemo testDemo = new TestDemo ();
System.out.println (testDemo.x);//0
}
}
Default values:
Int   0
Float  0.0
Double 0.0
String   null
Char null
Byte  0
Short  0

Non static variables are also called as instance variables or object level variables
Non static variables will be stored in heap area as the part of object

2. Static variables:
If the value of a variable is not varying from object to object then declare that variables as static variables.
For static variables at class level a single copy will be created and shared across every object of that class
Whenever we declare variable as static we can access that variables without creating object and we can access by directly, class name and by object
Access specifier static Datatype variable name = variable value;
When static variables will be created?
Static variables will be created at the time of class loading and destroyed at the time of class unloading
When do we declare a variable as static::
=======================================
Whenever the value of a variable is not changing from object to object then declare such type of variables as static variables

Access scope
=============:
 We can access static variables  throughout our program
Where do we declare static variables?
After starting the class immediately we will declare non static variables directly with static keyword
Default values:
Initialisation of static variables is optional. If we don’t perform initialisation of variables JVM is going to assign the default values.
Ex:   public class TestDemo
{
Static String x;
Public static void main(String[] a){
System.out.println (x);//null
}
}
Static variables are also called as class level variables
Static variables will be stored inside method area

3. Local variables:
Whenever we declare variables inside the method or constructors such type of variables we can call it as local variables.
When local variables will be created?
Local variables will be created at the time method or constructor creation and method or constructor execution is done local variables will be destroyed
When do we declare a variable as local:
=======================================
Whenever you need the values for certain portion of area to meet requirements then declare that variables as local variables

Access scope
=============:
 We can access local variables within that method or constructor but not outside of that
Where do we declare local variables?
We need to declare local variables within the method or constructors and blocks
Default values:
For local variables compulsory we must and should perform initialization otherwise we will get compile time error. I.e. For local variables JVM won’t provide any default values.
Ex:   public class TestDemo
{
Public static void main(String[] a){
Static String x;
System.out.println(x);//compile time error
}
}

Local variables will be stored in stack memory
Local variables are also called as temporary variables or automatic variables or stack variables
4.Object type variables:
By using object type variables we can refer objects.

SeleniumDemo demo = new SeleniumDemo();

No comments:

Post a Comment