You are on page 1of 27

CSE310: Programming in Java

Topic: Variables & their Scope


Outlines [Expected Time: 1 Hours]

• Variables and their Scope


• Local Variables
• Instance Variables
• Class Variables
Variables
 Variables are the data members or the fields defined
inside a class.

 There are three types of variables:


 Local Variables
 Instance Variables
 Class Variables
Local Variables
 Local variables are those data members which are
declared in methods, constructors, or blocks.

 They are created only when the method, constructor or


block is executed and destroyed as soon as the
execution of that block is completed.

 So the visibility of local variables is inside the block


only, they can’t be accessed outside the scope of that
block.
Important

 Access modifiers are NOT ALLOWED with local variables.

 Local variables are created in STACK.

 Default value is NOT ASSIGNED to the local variables in


Java.
Instance Variables
• Variables which are declared in a class, but outside a
method, constructor or any block are known as instance
variables.

• They are created inside the object in heap.

• Each object has its own set of instance variables and


they can be accessed/modified separately using object
reference.
• Instance variables are created when an object is created
with the use of the 'new' keyword and destroyed when the
object is destroyed.

• Access modifiers can be used with instance variables.

• Instance variables have default values (Initialization is not


mandatory, JVM will take the default value)

• For numbers the default value is 0,


• for Booleans it is false
• and for object references it is NULL.
Class (Static) Variables
 Class variables are also known as static variables.

 Variable which are declared in a class, but outside a method,


constructor or a block and qualified with ‘static’ keyword are known
as class variables.

 Used when we don’t want to modify the value from object to object.

 Only one copy of each class variable is created, regardless of how


many objects are created from it.

 Static variables can be accessed by calling with the class name.


ClassName.VariableName
 Static variables are created with the start of execution of a
program and destroyed when the program terminates.

 Default values are same as instance variables.

 A public static final variable behaves as a CONSTANT in


Java.

 Static variables can be initialized using static block also.


Example
class A
{  
int data=50;//instance variable  
static int m=100;//static variable  
void method()
{  
int n=90;//local variable  
}  
}//end of class  
Variable Initialization
Local variables must be initialized explicitly by the
programmer as the default values are not assigned to them
where as the instance variables and static variables are
assigned default values if they are not assigned values at the
time of declaration.
Brainstorming 1
What will be the output of the following Program?

class VariableDemo
{
public static void main(String [] rk)
{
int x = 10;//if used without initialization will give error(int x)
System.out.print(x);
}
}
Brainstorming 2
What will be the output of the following Program?

class VariableDemo
{
static int x;
public static void main(String [] rk)
{
System.out.print(x);
}
}
Variable Using Object
class VariableDemo
{
int x=100;
public static void main(String [] rk)
{
VariableDemo v1=new VariableDemo();
System.out.print(v1.x);
}
}
Different Object References
class VariableDemo
{
int x=100;
public static void main(String [] rk)
{
VariableDemo v1=new VariableDemo();
System.out.print(v1.x);
VariableDemo v2=new VariableDemo();
v2.x=200;
System.out.print(v2.x);
VariableDemo v3=new VariableDemo();
v3.x=400;
System.out.print(v3.x);
}
}
Brainstorming 3
What will be the output of the following Program?

class VariableDemo
{
static int x;
public static void main(String [] rk)
{
System.out.print(VariableDemo.x);
//OR System.out.print(x);
}
}
class Abc
{
static String college=“LPU";//static variable
static int x=10;//static variable
int std_id;//instance variable
public static void main(String a[])
{
Abc a1=new Abc();
Abc a2=new Abc();
a1.std_id=1;
a2.std_id=2;
System.out.println(a1.std_id);
System.out.println(a2.std_id);
System.out.println(Abc.x);
System.out.println(college);//no need to call through classname, because it is static and called in the
static method i.e. main
}
}
Class VarDemo
{
static int i=0;
VarDemo()
{
i++;
System.out.println(i);
}
Public static void main(String a[])
{
VarDemo v1=new VarDemo();
VarDemo v2=new VarDemo();
VarDemo v3=new VarDemo();
VarDemo v4=new VarDemo();
VarDemo v5=new VarDemo();
VarDemo v6=new VarDemo();
}
}
Data Types
• Data types represent the different values to
be stored in the variable.
• In Java, there are two types of data types:
1.Primitive data types
2.Non-primitive data types
Why char uses 2 byte in java and what is \u0000 ?
It is because java uses Unicode system than ASCII code system.
The \u0000 is the lowest range of Unicode system.
Java Variable Example: Add Two Numbers
class Simple
{  
public static void main(String[] args)
{  
int a=10;  
int b=10;  
int c=a+b;  
System.out.println(c);  
}
}  
Output:
20
Java Variable Example: Widening
class Simple
{  
public static void main(String[] args)
{  
int a=10;  
float f=a;  
System.out.println(a);  
System.out.println(f);  
}
}   Output:
10
10.0
Java Variable Example: Narrowing (Typecasting)
class Simple
{  
public static void main(String[] args)
{  
float f=10.5f;  
//int a=f;//Compile time error  
int a=(int)f;  
System.out.println(f);  
System.out.println(a);  
}
}   Output:10.5
10
Java Variable Example: Overflow
class Simple
{  
public static void main(String[] args)
{  
//Overflow  
int a=130;  
byte b=(byte)a;  
System.out.println(a);  
System.out.println(b);  
}
}  Output:130
-126
Java Variable Example: Adding Lower Type
class Simple
{  
public static void main(String[] args)
{  
byte a=10;  
byte b=10;  
//byte c=a+b;//Compile Time Error: because a+b=20 will be int  
byte c=(byte)(a+b);  
System.out.println(c);  
}
}   Output: 20

You might also like