• Post Reply Bookmark Topic Watch Topic
  • New Topic

Any work-around on "Cannot refer to an instance field x while explicitly invoking a constructor" ?  RSS feed

 
Myke Enriq
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MyClass{
int x = 5;

MyClass(){
this("" + x); <----- compiler error
}

MyClass(String s){
System.out.println(x);
}

}


The compiler error I get is "Cannot refer to an instance field x while explicitly invoking a constructor".

My purpose is to see at what point in time exactly x is allocated memory and initialized by default with 0.

Could this error be because x does not exists yet ? Meaning the memory for x is allocated by some default compiler inserted code just after the return from super() ? Does anyone know if this is true ?

Anyhow , if there is any work around and somehow you manage to print x , it would bring me close to an answer/


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Not really a programming diversion question anymore -- moving topic to the general java forum.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Myke Enriq wrote:
My purpose is to see at what point in time exactly x is allocated memory and initialized by default with 0.

Could this error be because x does not exists yet ? Meaning the memory for x is allocated by some default compiler inserted code just after the return from super() ? Does anyone know if this is true ?



No. Memory allocation for instance variables of the class (and the super classes) are done together. And is done *before* the initialization step -- which includes the super() constructor call.

See section 12.5 of the Java Language Specification... http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.5

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Myke Enriq wrote:class MyClass{
int x = 5;

MyClass(){
this("" + x); <----- compiler error
}

MyClass(String s){
System.out.println(x);
}

}


The compiler error I get is "Cannot refer to an instance field x while explicitly invoking a constructor".

Anyhow , if there is any work around and somehow you manage to print x , it would bring me close to an answer/



As for why this restriction ... see section 8.8.7.1 of the Java Language Specification...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.7.1

To work around it, meaning to see the variable before it is initialized to 5... well... one option would be to create a static method that takes a myClass instance, and pass it for printing. Of course, this method probably needs to return a dummy string to allow it to be executed at the point you want it.

Henry
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!