hi to all,i have a question on static block.
i saved below as test.java


class test extends a
{
static {
System.out.println("test static");
}
public static void main(String []dfs)
{


}



}
class a
{
static
{
System.out.println("a static");
}
public static void main(String []asdf)
{

}

}

o/p
as static
test static

but if i change base class as test class then

class test
{
static {
System.out.println("test static");
}
public static void main(String []dfs)
{


}



}
class a extends test
{
static
{
System.out.println("a static");
}
public static void main(String []asdf)
{

}

}
o/p
test static

explain me why "a static" wasn't print in second code when
it is in derived class



hi to all,i have a question on static block. i saved below as test.java class test extends a ..

Answer / ashish

just chek again ur code

class test extends a
{
static {
System.out.println("test static");
}
public static void main(String []dfs)
{


}



}
class a
{
static
{
System.out.println("a static");
}
public static void main(String []asdf)
{

}

}

it;s genrate the o/p
a static


class test
{
static {
System.out.println("test static");
}
public static void main(String []dfs)
{


}



}
class a extends test
{
static
{
System.out.println("a static");
}
public static void main(String []asdf)
{

}

}



test static
a static


ok jst analyze ur problem thn contact me
ashish.gupta126126@gmail.com

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More Core Java Interview Questions

How is object created in java?

5 Answers   Cap Gemini,


When arithmeticexception is thrown?

0 Answers  


What is arraylist e in java?

0 Answers  


Can we extend the String class?

3 Answers   Truworth,


Can final class have constructor?

0 Answers  






What is JFC?

0 Answers  


Can an interface extend another interface?

0 Answers  


Is map ordered in java?

0 Answers  


Do we need to manually write Copy Constructor?

0 Answers   HCL,


What are the traverses in Binary Tree?

2 Answers   Adobe, Infosys,


What is the difference between static and global variables and also define what are volatile variables?

0 Answers   Flextronics, Hexaware,


What is difference between add() and addelement() in vector?

0 Answers  


Categories