my dog learned polymorphism*
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 Android Security Essentials Live Lessons this week in the Android 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: 2277
    
  28

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: 4523
    
    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.
 
jQuery in Action, 2nd edition
 
subject: how this program works i didn't understand
 
Similar Threads
var args method
Are the rules regarding timing of initialization of final and non-final fields different?
Broader exceptions thrown by methods that implement interfaces
Illegal start of expression error
what really it mean's