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

Static Block

Static blocks are executed when a class is loaded by the Java Virtual Machine (JVM). They allow initialization code to be run without needing a main method. Static blocks are executed from top to bottom in the order they are defined. It is possible to print to the console without a main method by using static blocks or by invoking static methods during class loading. The static block is executed once during class loading, while instance blocks are executed each time an object is created.

Uploaded by

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

Static Block

Static blocks are executed when a class is loaded by the Java Virtual Machine (JVM). They allow initialization code to be run without needing a main method. Static blocks are executed from top to bottom in the order they are defined. It is possible to print to the console without a main method by using static blocks or by invoking static methods during class loading. The static block is executed once during class loading, while instance blocks are executed each time an object is created.

Uploaded by

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

static block

----------------------

->static blocks will be executed at the time of class loading,hence


at the time of class loading if we want to perfrom any activity
that should be defined in static block

->at the time of java class loading the corresponding native libraries
should be loaded,hence we have to define this activity inside
static block

example:
class Test
{
static
{
System.loadLibrary("native library path");
}
}
->within a class we can define any number of static blocks,but
all these static blocks are executed from top to bottom(in the order they are
defined)

****without writing main method is it posible to print a statement on the console?


ans: YES using static blocks it is posiible

ex:
class test
{
static
{
System.out.println("hello");
}
}
o/p:hello
exception:nosuchmethoderror resolved by system.exit()

class tets
{
static
{
System.out.println("hello");
System.exit(0);//the control stops here jvm wont search for main method
}
}
o/p:hello

****without writing main method and static block is it posible to print a statement
on the console?
ex:
class test
{
static int x=m1();//here m1 should be executed so control goes to m1
public static int m1()
{
System.out.println("hiii");
System.exit(0);
return 10;
}
}
o/p:hiiii

ex2:
class test
{
static test t=new test();//here obj is craeted and at the time of obj creation
instance block is executed
{
System.out.println("hii");
System.exit(0);
}
}

ex3:
class test
{
static test t=new test();//here constructer is called
test()
{
System.out.println("hii");
System.exit(0);
}
}

yes it is possible to print something without main method until 1.6


from 1.7 main is mandatory

static control flow in parent to child relationship


--------------------------------------------------------

whenever we are executing child class the following are the sequence
of events will be exeuted automatically as a part of static control flow
1.Identification of static mem from parent to child[1 to 11 in prog]
2.execution of static var assignments and static blocks from parent to child[12 to
22]
3.execution of only child class main method[23 to 25]
** whenever we are loading child class parent class will be loaded automatically
but when we are loading parent -child will not be loaded because parent class
memberes by default availale to the
child class whereas child class members by default wont availae to the parent
---------------------------------------------------------
-> when we are executing a java class 1st static control flow is executed
in that static control flow if we are creating object then for every creation of
object the following sequence of events happen:
1.identification of instance members from top to bottom[3 to 8]
2.execution of instance variable assignments from top to bottom[9 to 4]
3.execution of constructors[15]

***Static control fow is 1 timeactivity which will be performed at the time of


class loading but instance control flow is not 1 time activity and it will be
performed
for every object creation
***object creation is most costly operation if there is no specific reqirement
then it is not recommended to create object

instatnce control flow in parent to child relation ship


----------------------------------------------------------
whenever we are creating child class object the following sequence of events
will be performed automatically as a part of instance control flow :
1.identification of instance members from parent to child[4 to 14]
2.execution of instance variable assignments and instance blocks only in
parent class[15-19]
3.execution of parent constructor [20]
4.execution of instance variable assignments and instance blocks in child class[21-
26]
5.execution of child constructor[27]

***** from static area we cant access instance members directly because
while executing static area jvm may not identify instance members

****in how many ways we can create an object in java or in how many ways we can get
object in java? in part 13
1.by using new operator Test t =new Test()
2.by using new instance() method
--------------------
within the static block if a read operation performed then it is direct read and if
that block calls a method where read operation performed it is indirect read
-> if a variable is just identified ny the jvm and the original value not yet
assigned then the variable is said to be in read indirectly and write only
state(RIWO)
->if a variable is in RIWO state then we cant perform direct read but we can
perform indirect read
->if we are trying to read directly then we get compile time error saying illegal
forward reference

You might also like