0% found this document useful (0 votes)
13 views25 pages

Static Keyword

The execution of any program begins from the main method. There are three types of static components: 1) static variables, 2) static blocks, and 3) static methods. The static components are loaded into the static segment by the class loader before any objects are created. The static block gets executed before the main method. Static components can be accessed without creating an object, while non-static components require object creation. The static block is useful for initializing static variables before they are used.

Uploaded by

Suma Ph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views25 pages

Static Keyword

The execution of any program begins from the main method. There are three types of static components: 1) static variables, 2) static blocks, and 3) static methods. The static components are loaded into the static segment by the class loader before any objects are created. The static block gets executed before the main method. Static components can be accessed without creating an object, while non-static components require object creation. The static block is useful for initializing static variables before they are used.

Uploaded by

Suma Ph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Question

Execution of any program will begin


from where.?
There are 3 types of static components
1)Static variable
2)Static blocks
3) Static methods
Flow of execution of program
class Demo
{ static methods()3
Static variables 1
Non static variables4 non static methods() 6
Static block 2
{}
Non static block 5 }
{}
Who can access whom
Static variable 1
Static block 2
Static methods 3

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; {

static{ Farmer f1=new Farmer();


foi=2.0f Farmer f2=new Farmer();
} Farmer f3=new Farmer();
void acceptInput()
{ f1.acceptInput();
Scanner scan=new Scanner(System.in); f2.acceptInput();
System.out.println("enter the value of p and t"); f3.acceptInput();
p=scan.nextInt();
t=scan.nextInt(); f1.compute();
roi=2.0f; f2.compute();
} f3.compute();
void compute()
{ f1.disp();
si=(p*t*roi)/100; f2.disp();
} f3.disp();
void 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() roi 0 2.0
{find si} si 0 10.0 static segment
Static int roi=2.0
void 5000
Static {
acceptInput()
p 0 2000 t 02 r=2.0f
{-------;}
}
si 0 80.0
p s v m() P s v m()
f1 4000 6000 {
0 }
f2 5000 p 0 3000 t 03
f3 6000
si 0 180.0
JVM

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

Class Object class Dog {


class LaunchDog {
{ String name;
p s v m(string args[]) {
Object(){ String color;
Dog d1=new Dog();
----------------- Int cost;
Dog d2=new Dog();
----------------- Static int count;
Sop(Dog.count);
-------} Dog d3=new Dog(“tommy”,”black”,”9999”);
} Sop(Dog.count);
Dog()
}}
{ super();
count++}

Dog(String name,String color,int


cost) {
Super(); Output: 2
this.name=name; 3
this.color=color;
this.cost=cost;
count++}
Heap segment

name 0 asd name 0 sdf

colour 0 asd colour 0 dfg

d1 cost 0 123 d2 cost 0 999


4000 5000
count 0 1 count 0 1

name 0 asd

colour 0 asd
d3 cost 0 123
6000
count 0 1
Heap segment

name 0 asd name 0 sdf

colour 0 asd colour 0 dfg

d1 cost 0 123 d2 cost 0 999


4000 5000

static segment

name 0 asd count 0 1 2 3

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(); }

Dog(String name,String color,int


cost) { Output: 2
Super(); 3
this.name=name;
this.color=color;
this.cost=cost;
• Non static block will get executed immediately
after the execution of super method.
• If there are multiple non static blocks then it
will get executed according to the sequence of
blocks occurance.
class Demo1
Class Object { Class LaunchDemo
{ { {
Object(){ main()
System.out.println(“inside non static block1”);
------------------- {
} Demo d=new Demo();
------------------- {
---} }
System.out.println(“inside non static block2”); }
}
}
Demo1()
{
//Super()
System.out.println(“inside a constructor”);
}
}
applications
• Whenever a common copy of variable has to be
maintained for multiple objects then we should
use static variable.
• Whenever a individual copy of variable for
individual object has to be maintained then we
should declare that variable as non static.
• Whenever there are set of statements that has to
be executed before execution of main method
then we should write those statements within
static block.
applications
• Whenever there are certain set of statements
that compulsorily has to be executed for all the
object then we should write it in non static block.
• Whenever we want to execute any method
without creation of object then we should make
that respective method as static method.
• Whenever we want to execute any method after
creation of object then we should make that
respective method as non static method.
Difference between static and non
static methods
Static methods Non static methods
These are also known These are also known
as generic or utility as Specific methods.
methods. They cannot be
they can be invoked invoked without
without creating an creating an object.
object. EX:nextInt
EX:currentThread() method,nextFloat
method
Difference between static and non
static variables
Static Variables Non static variables
• memory for static variables • memory for static variables in
in allocated on the static allocated on the heap
sgmt.
segment.
• the static variables are
initialized to default values • the static variables are
by JVM. initialized to default values by
• common copy for all objects. Object class constructor.
• they can be accessed by • Individual copy for one object.
using class name. • they cannot be accessed by
• they are created by using using class name.
static keyword. • Static keyword not used for
• memory is not deallocated non static variables.
by garbage collector.
• memory is deallocated by
garbage collector.

You might also like