• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

doubt in mock exam!

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

When I compiled the following code, it is displaying pre post.
But I thought that the answer would be,

r1 r4 pre b1 b2 r3 r2 post. This question is from javacertifications.

Could you tell me what is the correct output?







 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is displaying r1 r4 pre b1 b2 r3 r2 post on my machine. What are you using to run this program??
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They will get printed surely. Perhaps you edited the question and java source by mistake ?
 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I run it by using NetBeans IDE 6.7 tool. But still it is the same.

run:
pre post
BUILD SUCCESSFUL (total time: 0 seconds)

Will you clarify my doubt?


 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use any IDE during SCJP preparation. Try to compile your programs from the command line...
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question too... (I have no problem with the output that I ran)

When the control enters main...should it now first see the print statement and print "pre" and continue with the rest of the output?
Then when it sees new C() .... call the contructor of B which in turn calls A's contructor.
Can somebody please reiterate the rules for running static/initialization block and contructors.

Thanks in advance.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Don't use any IDE during SCJP preparation. Try to compile your programs from the command line...




I agree with Ankit. IDEs tend to make things too easy. For learning purposes, use command line as this will make you think more about error messages etc.
 
Ranch Hand
Posts: 103
jQuery Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello every one,


I think I've figured out what's happening an how it's happening..

One thing is sure that before running the child class's constructor one of the constructors of the parent class must be executed.

When the class C is compiled,at that time B is also compiled and so is A class.
now when the class C is executed, then class B is loaded in to the memory.

Static blocks are executed, when the class is loaded in to the memory.
That's why r1 and r2 are printed first.(No constructor is executed yet!!!)

Now, the control is back to the System.out.println("pre"); statement of C class.

then "pre" is printed.

Now when C's constructor is executed, B's constructor must be executed which in turn invokes A's constructor.
As class A has a initialization block,that must be executed first before running it's constructor.

And rest is simple.

I hope it helps you.


 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Esther

When the class is loaded JVM will initialize the class at any of the following conditions.

If the static method of the class is called inthis case main
If the instance of the class is created
If the static variable is assigned a value.
If the non compile time constant is accessed

So in this case class C's main method is called by JVM so it has to initialize C but since C also extends B which extends A it will initalize the super class B and A before C .

Once it initializes all the class members and run static initalizer blocks in the upper tree it will now enter into main method and print pre then we are creating C instance which will call super constructor till Object and executes the initalizer and initializer block from Object to C.

Hope it helps.
 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for immediate response . If I use NetBeans for any program will it display any different output? Except this which I mentioned the above one I never get any problem so far.

There is only one command in to run a program( shift+f6) in NetBeans. It does n't have any separate command to compile.

I don't know is there any option to run a program at command line with NetBeans tool.


 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think it should alter the output however I haven't tried the code in NetBeans. I also agree with Ankit that instead of using an IDE you should use the commandline for compiling and running the program.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

i have the same problem (output "pre post") in Eclipse. But output is correct when moving the definition of class B to a separat file. There are some posts on Eclipse forums about that. I cannot explain this strange behavior. Dont use IDE in exam prep. Dont put multiple classes in one file when using an IDE.

Cheers
John
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic