Static Keyword
Static Keyword
Non-Static variable 4
Non-static block 5
Non – static methods 6
Code segment
class Demo static void disp1(){
{ print a,b,c;
static int a,b,c; }
int x,y,z; void disp2(){
static{ print a,b,c,x,y,z;}
a=10;b=100;c=1000; }
} class LaunchDemo{
{ p s v m(String a[]){
x=9,y=99,z=999; Demo.disp1();
} Demo d1=new Demo1();
d1.disp2();
Static segment
Stack segment
a 10
0staticbint
100
0 a,b,cc1000
0
static{
a=10,b=100,c=1000}
static disp1(){
void disp2() print a,b,c;}
{print a,b,c,x,y,z;}
p s v m(String ar[]){
----------------------;}
Output
10 static void disp1()
100 {print a,b,c;}
Heap segment
1000
10
100 x 0 9
1000 p s v m(String
y 0 99
9 ar[]){
99 0 d1
---------------------- z 0 999
999 ;} 4000
JVM
Class Loader
For any program first JVM get loaded on top of the stack, JVM will hand over the control
to CLASS loader and class loader will scan the entire code segment to check if there is
any static elements in the program or not if its available then the copy of the same will
be loaded on static segment.
All the static elements will start getting executed one by one .
Applications
1) Non static variable
2) Static variable
3) Static block
4) Non static block
5) Static methods
6) Non static methods
Applications of non static variable
/instance variable class LaunchFarmer
{
Program:class Farmer
public static void main(String[] args)
{
{
float p,t,roi;
float si;
Farmer f1=new Farmer();
Farmer f2=new Farmer();
void acceptInput()
Farmer f3=new Farmer();
{
Scanner scan=new Scanner(System.in);
f1.acceptInput();
System.out.println("enter the value of p and t");
f2.acceptInput();
p=scan.nextInt();
f3.acceptInput();
t=scan.nextInt();
roi=2.0f;
f1.compute();
}
f2.compute();
void compute()
f3.compute();
{
si=(p*t*roi)/100;
f1.disp();
}
f2.disp();
void disp()
f3.disp();
{
}
System.out.println(si);
}
}
}
Stack segment
Output
10.0
void disp() 80.0
{print si;} Heap segment
180.0
4000
p 0 1000 t 01
void compute()
{find si} r 0 2.0 si 0 10.0 static segment
P s v m()
void 5000 {
acceptInput() }
p 0 2000 t 02
{-------;}
r 0 2.0 si 0 80.0
p s v m()
f1 4000 6000
0
f2 5000 p 0 3000 t 03
f3 6000
r 0 2.0 si 0 180.0
JVM
Class Loader
Application of NON static variable
When there should be a individual copy of
variable for each objects then we should make
the variable as non static or instance(EX:p,t,si).
• In the above program the rate of interest is
common for all the farmer object therefore a
variable which is common for all the objects
should be declared as static.
• In the above program the memory is not
efficiently used.
Applications of static variable
Program:class Farmer class LaunchFarmer
{ {
float p,t,si; public static void main(String[] args)
static float roi; {
Class Loader
Application of static variable
A static variable should be used whenever a
common copy of variable should be used.
when there is common copy that needs to be
shared among multiple objects then we
should declare that kind of variable as static.
Advantages of static block
class Demo
{p s v m(string args[]) Output:
{ BJSHUB
sop(“BJSHUB”);
}
}
Advantages of static block
class Demo
In the program first we will
{p s v m(string args[]) Output: get NAVEEN as a output
{ NAVEEN and then BJSHUB as static
sop(“BJSHUB”); BJSHUB block will get executed
}
before main method.
static{
System.out.println(“NAVEEN”)
;
}
}
Application of static block
• Static block should be used whenever there
are certain set of statements that has to be
executed before execution of main method.
Non static / instance variables
name 0 asd
colour 0 asd
d3 cost 0 123
6000
count 0 1
Heap segment
static segment
colour 0 asd
d3 cost 0 123
6000
Non static / instance variables
Class Object class Dog {
{ String name;
String color; class LaunchDog {
Object(){
Int cost; p s v m(string args[]) {
-----------------
static int count; Dog d1=new Dog();
-----------------
Dog d2=new Dog();
-------}
{ Sop(Dog.count);
}
static count++; Dog d3=new Dog(“tommy”,”black”,”9999”);
} Sop(Dog.count);
}}
Dog()
{ super(); }