• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Block

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friends
Read this Code ..This is Question from jtips Mock exams
class Test {
Test() {
System.out.println("Super");
}
static {
System.out.println("Superclass - Static Initializer");
}
}
public class MQ1 extends java.lang.Object {
public static void main(String [] args)
{
MQ1 test = new MQ1("Java");
System.out.println("In Main method");
}
static {
System.out.println("Subclass - Static Initializer");
}
MQ1(String s) {
System.out.println(s);
}
}
Ans is

Subclass - Static Initializer
Java
In Main method
how??? becouse static block initialize when class is loaded static method or static block or static variable not a member variable of any class so they should execute according to their order .......
according to me answer should be
Superclass - Static Initializer
Subclass - Static Initializer
Java
In Main method
Can any one explaine me why this is not true......
Thnx in advance
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Deepak!
First of all: when you compile this code, two separate class files are created. One called Test.class and another called MQ1.class.
Since class MQ1 does not extend Test (it extends java.lang.Object), class Test is not loaded when you run this program. Only class MQ1 and class Object (not 100% sure on class Object).
As soon as you load class MQ1 the static initializer block is run. Then, in your main method, you create an object of the class which means that the constructor is run. Finally you output "In main method" in main.
So, I think the given answer is the correct one.
Hope this helps, and that I didn't miss anything crucial!
//Kaspar
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I agree with Kaspar. the result would be different if u extend the class Test.
Bhaskar
in that case the following is the output like u can very well guess.
Superclass - Static Initializer
Subclass - Static Initializer
Super
Java
In Main method

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I believe you have hit the nail on the head. With regard to whether Object is loaded, I would have thought that it must be as constructors are chained all the way up to Object + how else would you inherit methods from Object if it were never loaded!
Kind regards,
james
 
Deepak B
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnx
Kaspar james Bhaskar

 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your name 'Deepak B' does not comply with the JavaRanch naming policy. Please spare a moment and re-register with a name that meets the requirements.
Thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic