static block in java................
package staticblock;
public class Test {
{
System.out.println("ist instance block");
}
{
System.out.println("2nd instance block");
}
static
{
System.out.println("ist static block ");
}
static
{
System.out.println("2nd static block ");
}
public Test()
{
System.out.println("0- argument constructor");
}
public Test(int a)
{
System.out.println("1- argument constructor");
}
public static void main(String[] args)
{
Test t=new Test();
Test t1=new Test(10);
}
}
==============================================================================
o/p:------------
ist static block
2nd static block
ist instance block
2nd instance block
0- argument constructor
ist instance block
2nd instance block
1- argument constructor
===============================================================================================
package staticblock;
public class Test1 {
static int x;
static
{
x=100;
}
static void display()
{
System.out.println(Test1.x);
}
public static void main(String[] args) {
Test1.display();
}
}
==============================================================================================
o/p--------------
100
================================================================================================
package staticblock;
public class Test2 {
int x=100;//instance variable
static int y=200;//static variable
void m1(int a)//instance method
{
System.out.println("instance method");
}
static void m2(String s1)//static method
{
System.out.println("Static method");
}
Test2()
{
System.out.println("0-argument-constructor");
}
Test2(int a)
{
System.out.println("1-argument-constructor");
}
{
System.out.println("instance block");
}
static
{
System.out.println("static block");
}
public static void main(String[] args) {
//static block execute
Test2 t=new Test2();//instance block,constructor
Test2 t1=new Test2(10);//instance block,constructor
t.m1(10);//m1 method execute
Test2.m2("govind");//m2 method execute
}
}
==============================================================================================
o/p:----------------
static block
instance block
0-argument-constructor
instance block
1-argument-constructor
instance method
Static method
No comments:
Post a Comment