aspose file tools*
The moose likes Java in General and the fly likes Problem with output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with output" Watch "Problem with output" New topic
Author

Problem with output

Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
This is a sample question from oracle's website.



What is the result?
1. c
2. f
3. c f
4. an error, followed by f
5. f followed by an error
6. an error, followed by c f


The correct answer for this is option 5.
I wanted to ask why 5 and why not option 4.
I think that when the class is loaded static method doStuff() will be executed and then it will throw an error.And as finally clause is always executed(not considering System.exit(0) ) so it will print f.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rohan Deshmkh wrote:
I wanted to ask why 5 and why not option 4.


We call doStuff(), which throws an Error. We do not catch that Error. However, our call to doStuff() was in a try block, and that try has an associated finally. The finally block is executed, printinf "f", before the Error bubbles up to the caller, which in the case, the JVM. Any exception that's thrown from your main() is printed out by the JVM.


I think that when the class is loaded static method doStuff() will be executed


No. Methods aren't called until we call them. So when the class's main() is executed, it calls doStuff().

and then it will throw an error.And as finally clause is always executed(not considering System.exit(0) ) so it will print f.



Note that exceptions are only printed out when something explicitly prints them. Such as when we call printStackTrace() in our code, or when the JVM calls it for uncaught exceptions from our main(). Just because an exception is thrown, that doesn't mean anything will be printed.

So the finally block is executed, then the Error is passed up the stack to the caller, which in this case is the JVM, which then prints out the Error.
Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127


I think that when the class is loaded static method doStuff() will be executed



No. Methods aren't called until we call them. So when the class's main() is executed, it calls doStuff().



Ok thanks i got it, i got confused.So, Static blocks are executed first when the class is loaded first and not the static methods.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rohan Deshmkh wrote:
Ok thanks i got it, i got confused.So, Static blocks are executed first when the class is loaded first and not the static methods.


Correct. (Technically speaking, static initializers are executed when the class is initialized, which is a step that comes after loading, but in most cases, we can think of loading and initializing as a single step.)

Also, if you're not aware of them already, it's worth knowing that there are also instance initializers, which look like static initializer blocks, but without the "static" keyword. These are run every time an instance is created, before any constructor is invoked.
Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
Ok thanks.. I got it.
 
Consider Paul's rocket mass heater.
 
subject: Problem with output