Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic Java Program

 
kumar shiva
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you very much Mr.Jain
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic