Static Block
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)
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);
}
}
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]
***** 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