Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how this program works i didn't understand

 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


output--hi
hi from m1
hi from main
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic