aspose file tools
The moose likes Java in General and the fly likes Constructor not executing? Help !! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Constructor not executing? Help !!" Watch "Constructor not executing? Help !!" New topic
Author

Constructor not executing? Help !!

Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
In the following code, I was expecting text "In the first class!!" to get printed. It is not printing this text.

Can you please help me to understand this please ?

class tst
{
public static void main(String ara[])
{
tst t = new tst();
new first();

class first
{
first()
{
System.out.println("In the first class!!");
}
}

}
}
Derek Baker
Ranch Hand

Joined: May 23, 2003
Posts: 46
I can't even compile that, much less get the message you expect to print out. I believe it won't compile because the first class is defined within the same method that you call new first(). I think solving that problem comes first. I can get it to compile and print the message if I move the declaration for the "first" class outside of the declaration of the tst class. Like so:
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
The text is not printed because the code doesn't compile. :roll: The code doesn't compile because the class first is a local inner class, i.e. a local declaration. It must therefore be declared (defined) before it is referenced. Moving the call to the constructor first() to after the local class definition, as in Michael's example, allows the code to compile and produce the expected output.

Jules
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Constructor not executing? Help !!
 
Similar Threads
Two simpe methods calling each other not working as expected
Inner Class
default constructor
Object Creation
One more