aspose file tools*
The moose likes Beginning Java and the fly likes Basic Java Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic Java Program" Watch "Basic Java Program" New topic
Author

Basic Java Program

kumar shiva
Greenhorn

Joined: Jul 04, 2012
Posts: 12

Why compile error occurs at a1 object creation(2nd line)

public class Example {
Example a1= new Example();
public static void main(String args[]){
Example a2 =new Example();
}
}
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

What is the exact and complete text of the compiler error?

Update: This is even MORE important now that I have copied your code and managed to compile it exactly as it is written here.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

kumar shiva wrote:
Why compile error occurs at a1 object creation(2nd line)

There are no compiler error in that code.. Rather a StackOverflowError.
Notice that every instance of you class Example has a new instance of the same class..

This becomes something like this: -

a2 (has) a1 (has) a1 (has) a1 (has) a1 (has) a1 (has) a1 ........... and so on...

So, every instance of Example has another instance of Example, and it keeps going, until StackOverflowError in reached..

*NOTE:- All the a1's above are not the same, they are different instance pointing to different objects.. Since they are instance reference..
kumar shiva
Greenhorn

Joined: Jul 04, 2012
Posts: 12

Thank you very much Mr.Jain
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

kumar shiva wrote:
Thank you very much Mr.Jain

You're welcome.. But also consider giving answer to what Fred has asked..
If somehow you got the compiler error.. then post it here (full compiler error)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Basic Java Program