0% found this document useful (0 votes)
4 views

Lecture 8. Java Fundamentals - Types of Variables

Uploaded by

555bsyadav555
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 8. Java Fundamentals - Types of Variables

Uploaded by

555bsyadav555
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

Types of Variables:-
Types or Divisions of Variables:
1. First Division:-
Based on type of value, represented by a variable, all variables are divided into two
types.
i. Primitive Variables:-
They can be used to represent primitive values.
These kinds of variables are mutable.
Ex.:-
int x = 10;
ii. Reference variables:- They can be used to refer objects.
Ex.:-
Student s = new Student();
Here, s is referencing or pointing an object or location of Student type.
Reference variables are mutable.

2. Second Division:-
Based on declaration and behaviour all variables are divided into 3 types.
a. Instance Variables
b. Static Variables
c. Local Variables

a. Instance Variables:-
➢ If the value of a variable is varied from object to object, such types of
variables are called instance variables.
➢ For every object a separate copy of instance variables will be created.
➢ Instance variables should be declared within the class directly but outside
of any method or block or constructor.
➢ Instance variable will be created at the time of object creation and
destroyed at the time of object destruction. Hence the scope of instance
variable is exactly same as the scope of object.
➢ Instance variable will be stored in the heap memory as the part of object.
➢ We cannot access instance variables directly from static area but we can
access by using object reference.
➢ But, we can access instance variables directly from instance area (instance
Methods).
Ex.:-
class Test{
int x = 10;
public static void main(String[] args){
System.out.println(x);// CTE: non-static variable x cannot be
referenced from a static context.
Test t = new Test();
System.out.println(t.x);// 10
}
public void m1(){
System.out.println(x);// 10
}
}
For instance variables, JVM will always provide default values and do not require
performing initialization explicitly.
Ex.:-
class Test{
int x;
double d;
Boolean b;
String s;
public static void main(String[] args){
Test t = new Test();
System.out.println(t.x);// 0
System.out.println(t.d);// 0.0
System.out.println(t.b);// false
System.out.println(t.s);// null
}
}
Instance variables are also known as object level variables or attributes.

b. Static Variables:-
If the value of a variable is not varied form object to object, then it is not
recommended to declare variable as instance variable. We have to declare such
types of variables at class level by using static modifier.
In the case of instance variables, for every object a separate copy will be created
but in the case of static variables a single copy will be created at class level and
shared with every object of the class.
Static variables should be declared within the class directly but outside of any
method or block or constructor.
Static variables will be created at the time of class loading, and destroyed at the
time of class unloading. Hence scope of static variable is exactly same as scope of
that class.
Process of class loading and unloading:- Use following command to execute
program:- java Test
1. JVM will start
2. Main thread will be created
3. Locate Test.class file
4. Load Test.class (Static Variable Creation)
5. Execute main() method
6. Unload Test.class (Static Variable Destruction)
7. Terminate Main Thread
8. Shutdown JVM
Static variables will be stored in method area.
We can access static variables either by object reference or by class name but
recommended to use class name.
Within the same class it is not required to use class name and we can access
directly.
Ex.:-
class Test{
static int x=10;
public static void main(String args[]){
Test t = new Test();
System.out.printn(t.x);// 10
System.out.printn(Test.x);// 10
System.out.printn(x);// 10
}
}
We can access static variables directly from both instance and static areas. Because
at class loading time static variable takes place in the memory and ready to use.
Ex.:-
class Test{
static int x=10;
public static void main(String args[]){
System.out.printn(x);// 10
}
public void m1(){
System.out.printn(x);// 10
}
}

For static variables, JVM will provide default value and we are not required to
perform initialization explicitly.
Ex.:-
class Test{
static int x;
static float f;
static String s;
public static void main(String args[]){
System.out.printn(x);// 0
System.out.printn(f);// 0.0
System.out.printn(s);// null
}
}
Static variables are also known as class level variables or fields.
Ex.:-
class Test{
static int x=10;
int y=20;
public static void main(String args[]){
Test 1t = new Test();
t1.x=888;
t1.y=999;
Test t2 = new Test();
System.out.println(t2.x+” “+t2.y);// 888 20
}
}
c. Local Variables:-
Sometimes to meet temporary requirements of the programmer we can declare
variables inside a method or block or constructor. Such types of variables are called
local variables or temporary variable or stack variable or automatic variables.
Local variables will be stored inside stack memory.
Local variables will be created while executing the block in which we declared it.
Once block execution completes, automatically local variable will be destroyed.
Hence the scope of local variable is based on block in which we declared it.
Ex. 1:-
class Test{
public static void main(String args[]){
int i=0;
for(int j=0; j<3;j++){
i=i+j;
}
System.out.println(i+” “+j);
}
}
Output:-
CTE: cannot find symbol (symbol: variable j, location: class Test)
Ex. 2:-
class Test{
public static void main(String args[]){
try{
int j = Integer.parseInt(“Ten”);
}
catch(NumberFormatException e){
j=10;
}
System.out.println(j);
}
}
Output:-
CTE: cannot find symbol (symbol: variable j, location: class Test)

For local variables JVM would not provide default values compulsory we should
perform initialization explicitly before using that variable i.e. if we are not using
then it is not required to perform initialization.
Ex. 1:-
class Test{
public static void main(String args[]){
int i;
System.out.println(“Hello”);
}
}
Output:-
Hello
Ex. 2:-
class Test{
public static void main(String args[]){
int i;
System.out.println(i);
}
}
Output:-
CTE: variable i might not have been initialized.

Ex. 3:-
class Test{
public static void main(String args[]){
int i;
if(args.length>0){
i=10;
}
System.out.println(i);
}
}
Output:-
CTE: variable i might not have been initialized.

Ex. 4:-
class Test{
public static void main(String args[]){
int i;
if(args.length>0){
i=10;
}
else{
i=20;
}
System.out.println(i);
}
}
Output:- Based on command line arguments
Run Command:- java Test A B
o/p: 10
Run Command java Test
o/p: 20
Note 1:- It is not recommended to perform initialization for local variables inside
logical blocks because there is no guarantee for the execution of these blocks
always at runtime.

Note 2:- It is highly recommended to perform initialization for local variable at the
time of declaration at least with default values.

The only applicable modifier for local variable is final. By-mistake if we try to apply
any other modifier then we will get compile time error. Because local variable can
be accessed within the block only.
Ex.:-
class Test{
public static void main(String args[]){
public int x1 = 10;// Invalid
public int x2 = 10; // Invalid
public int x3 = 10; // Invalid
public int x4 = 10; // Invalid
public int x5 = 10; // Invalid
public int x6 = 10; // Invalid
final int x7=10;// Valid
}
}
Output:-
CTE: Illegal start of expression
Note:- If we do not declare any modifier then by default it is default. But this rule
is applicable only for instance and static variables but not for local variables.
Ex.:-
class Test{
int x=10;// Default access modifier: default
static int y=20; // Default access modifier: default
public static void main(String args[]){
public int z = 30;//Default modifier: Nothing
}
}
Conclusions:-
1. For instance and static variables JVM will provide default values and we do
not require performing initialization explicitly but for local variables JVM
does not provides default values. We must perform initialization explicitly
before using that variable.
2. Instance and static variables can be accessed by multiple threads
simultaneously and hence these are not thread safe. But in the case of local
variables, for every thread a separate copy will be created and hence local
variables are thread safe.

Type of variables Is Thread Safe?

Instance Variables No

Static Variables No

Local Variables Yes

3. Every variable in java should be either instance or static or local. Every


variable in java should be either primitive or reference hence various
possible combinations of variables in java are:-
Ex.:-
class Test{
int x=10; // Primitive Type
static String s = “Java”; // Reference Type
public static void main(String args[]){
int[] z = new z[3]; // Reference Type
}
}
Uninitialized Instance Arrays:-
Ex.:-
class Test{
int[] x;// Uninitialized Array
public static void main(String args[]){
Test t = new Test();
System.out.println(t.x);//null
System.out.println(t.x[0]); // RTE: null pointer exception
}
}
Note:- Once we creates an array, every array element by default initialized with
default values. Although it is instance or static or local array.
Initialized Instance Arrays:-
Ex.:-
class Test{
int[] x = new int[3];// Initialized Array
public static void main(String args[]){
Test t = new Test();
System.out.println(t.x);//[I@3e25a5
System.out.println(t.x[0]); // 0 (Default value zero)
}
}

Uninitialized Static Arrays:-


Ex.:-
class Test{
static int[] x;// Uninitialized Static Array
public static void main(String args[]){
Test t = new Test();
System.out.println(t.x);//null
System.out.println(t.x[0]); // RTE: null pointer exception
}
}

Initialized Static Arrays:-


Ex.:-
class Test{
static int[] x = new int[3];// Initialized Static Array
public static void main(String args[]){
Test t = new Test();
System.out.println(t.x);//[I@3e25a5
System.out.println(t.x[0]); // 0 (Default value zero)
}
}
Uninitialized Local Arrays:-
Ex.:-
class Test{
public static void main(String args[]){
int[] x;// Uninitialized Local Array
System.out.println(x);// CTE: Variable x might not have
been Initialized.
System.out.println(x[0]); // CTE: Variable x might not have
been Initialized.
}
}

Initialized Local Arrays:-


Ex.:-
class Test{
public static void main(String args[]){
int[] x = new int[3];// Initialized Local Array
System.out.println(x);// [I@3e25a5
System.out.println(x[0]); // 0 (Default value zero)
}
}

You might also like