Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

StackOverflow error while instantiating a class  RSS feed

 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • 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
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • 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
  • 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....
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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"?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably want to make those public final member variables static.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!