Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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: 13074
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)
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic