wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how this program works i didn't understand Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how this program works i didn Watch "how this program works i didn New topic
Author

how this program works i didn't understand

Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100


output--hi
hi from m1
hi from main
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2052
    
  22

Ritesh,

Please UseCodeTags while posting. I have added code tags for you

The static initializer of the class calls the constructor that calls m1. The static initializer is called as soon as the class is loaded, which happens before the main method is executed
Jacob Morehouse
Greenhorn

Joined: Feb 22, 2012
Posts: 17
Hi Ritesh,

You have a couple things going on, I'll try to walk you through. First, spacing is goofy... makes it hard to for you to debug (using NetBeans, alt+shift+f will fix spacing).

This can't exist here, can't instantiate an object of the class from within the class:


You have to put that code in the main, like this:



Hope that helps,

Jake
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

Jacob Morehouse wrote:This can't exist here, can't instantiate an object of the class from within the class:

Yes you can, just so long as it's a static member. Attempting to unconditionally initialize an instance member or a constructor-local variable of the same type would lead to a StackOverflowError.


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how this program works i didn't understand
 
Similar Threads
var args method
Illegal start of expression error
Are the rules regarding timing of initialization of final and non-final fields different?
Broader exceptions thrown by methods that implement interfaces
what really it mean's