• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StackOverflow error while instantiating a class

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI all
i am having a class with the following constructors. when i tried to instantaite it i am getting gettting stack over flow error .

Any idea where i went wrong??



Error ack = new Error();

gives me stackover flow error
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't see why you'd get a StackOverflowError. However, writing a class called "Error" is a decidedly bad idea and might be causing you problems.

There is a built-in class called "Error" in the java.lang package. That package is automatically imported into all code.

You should never name your classes the same as any class in java.lang .

You should generally avoid naming your classes the same as any built-in class from any package, too, though this is more an advice than a hard-and-fast rule.

Additional (16 Aug) Ah, later posters have identified that the problem is absence of "static" in those lines which construct instances of Error.
[ August 16, 2006: Message edited by: Peter Chase ]
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Class name Errror is just a fake name i given while i posted my code.

i got the cause of problem but donno why??

The reason is when i instantiating the instance variable is trying to get initialized by creating a instance of the class Error, so when it tries to do so i am getting stack overflow error.

I think its possible to have a reference to the class itself i donno why it is throwing stack overflow error...
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An inline non-constant declaration and assignment of a field is compiled to run in an initialiser - static or instance initialiser depending on the declaration. Instance initialisers run at construction time (before the constructor).

You have an inline non-constant declaration and assignment of a field whose type is also the type that it is declared within. This means that the resulting initialiser will run at construction time and when the initialiser runs, it will run the constructor that is explicitly called, and when that constructor is called, the initialiser will run, and when the initialiser runs, it will run the constructor that is explicitly called, and... ad infinitum, hence stack overflow.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony
Is there any way i can acheive the above functionality like having a instance variale holding the reference to the instance....
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Parameswaran Thangavel:
Hi Tony
Is there any way i can acheive the above functionality like having a instance variale holding the reference to the instance....



I don't understand what you actually want to achieve.

Why does it need to be an *instance* variable?

What exactly do you mean by *the* instance? Which "the"?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably want to make those public final member variables static.
 
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic