Types of Variables:
- Local
- Instance
- Transient
- Volatile
- Static
- Final
What is Local Variable?
The variables created inside a method or block are called local variables.
Local variables can not be accessed from another method. Because its scope is restricted only within its method.
Example
package easy; class test{ public void show(){ System.out.println("a:"+a); //Compile time error: cannot find symbol a. } public static void main(String [] args){ int a=10; System.out.println("a:"+a); } }Try it Yourself
What is Instance variables?
Instance variable gets life when object is created. It is destroyed when object is destroyed.
Object is destroyed when it is unreferenced.
Its scope is the scope of the object, object is available only if its referenced variable is available
Example
package easy; class test{ int a=10; public static void main(String [] args){ test t= new test(); System.out.println("a:"+t.a); } }Try it Yourself
What is Transient variable?
The class level variable that has transient keyword in its definition is called transient variable.
Local variable can not be declared as transient.
It leads to Compile time error: Illegal start expression
Example
package easy; class test{ static transient int a=10; static transient int b=20; public static void main(String [] args){ transient int x=10; // compile time error : illegal start expression } }Try it Yourself
We declare variable as transient to tell to JVM that we do not want to store variable value in a file in object serialization. Since local variable is not part of object , declaring it as transient is illegal.
What is Volatile Variable?
The class level variable that has volatile keyword in its definition.
Local variable can not be declared as volatile.
It leads to Compile time error: Illegal start expression
Example
package easy; class test{ static volatile int a=10; volatile int b=20; public static void main(String [] args){ volatile int x=10; // compile time error : illegal start expression } }Try it Yourself
We declare variable as volatile to tell to JVM that we do not want to modify variable value concurrently by multiple threads.
If we declare variable as volatile multiple threads are allowed to change its value in sequence one after another one.
Note:We learn about static and final keywords shortly.
Solve this!!
double FLOAT = 2.5 ; System.out.println( FLOAT );
class variable_scope { public static void main(String args[]){ int x; x = 5; { int y = 6; System.out.print(x + " " + y); } System.out.println(x + " " + y); } }
Second print statement doesn’t have access to y , scope y was limited to the block defined after initialization of x. Exception in thread “main” java.lang.Error: Unresolved compilation problem: y cannot be resolved to a variable
- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling