Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Basic Java Program

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you very much Mr.Jain
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic