cannot referenced <instance data member> before super type constructr has been called
Code :
public class MyClass {
private final int DEFAULT_VALUE = 10;
private int instanceValue;
public MyClass() {
this(DEFAULT_VALUE);
}
public MyClass(int value) {
instanceValue = value;
}
}
this is my first time encountering this kind of error. can anyone explain it please.. any would be greatly appreciated :)
Re: cannot referenced <instance data member> before super type constructr has been ca
From the JLS: Classes
An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.
For example, if the first constructor of ColoredPoint in the example above were changed to:
ColoredPoint(int x, int y) {
this(x, y, color);
}
then a compile-time error would occur, because an instance variable cannot be used within a superclass constructor invocation.
Re: cannot referenced <instance data member> before super type constructr has been ca
excuse me, i appreciate your explanation, but youre english is way too beyond my grammar level, if its not bothering too much, can you make it simpler? and thanks for the link, its really usefull, though its a bit complex.
Re: cannot referenced <instance data member> before super type constructr has been ca
That's not my English, I just copied it from the JLS directly.
But basically what it's saying is that you can't use an instance variable before the super constructor has been called. Which means you can't use an instance variable when invoking another constructor. The basic idea is that the instance doesn't really exist before the super constructor is called (that's not exactly true but it gets the point across), so you can't use instance variables. You can use static variables though.
Re: cannot referenced <instance data member> before super type constructr has been ca
wow thats it!,
questions:
1.)
Quote:
super constructor has been called.
- does this refer to my construtor(s)? the two constructors?
2.) so it means that in my code above , the instance data member (DEFAULT_VALUE), somehow doesnt "YET" exist when that constructor was calling it?
i just want to clarify these thing, if im right with my understanding
Re: cannot referenced <instance data member> before super type constructr has been ca
Quote:
Originally Posted by
chronoz13
wow thats it!,
questions:
1.) - does this refer to my construtor(s)? the two constructors?
Sort of. Remember, in every constructor (that doesn't invoke another constructor like your no-args constructor does), there is an invisible call to super. So your code is really doing this:
Code java:
public class MyClass {
private final int DEFAULT_VALUE = 10;
private int instanceValue;
public MyClass() {
this(DEFAULT_VALUE);
}
public MyClass(int value) {
super()
instanceValue = value;
}
}
So basically, the instance doesn't yet exist until that call to super() happens.
Quote:
Originally Posted by
chronoz13
2.) so it means that in my code above , the instance data member (DEFAULT_VALUE), somehow doesnt "YET" exist when that constructor was calling it?
i just want to clarify these thing, if im right with my understanding
Yeah, pretty much. It's not entirely true, but it's how I look at it.
Re: cannot referenced <instance data member> before super type constructr has been ca
ok now, i think im getting to the point, just like what youve said, i tried using a static variable
Code :
public class NewClass {
private static int CLASS_DATA_MEMBER = 1;
public NewClass() {
this(CLASS_DATA_MEMBER);
}
public NewClass(int x){
}
}
what i notice, in my own opinion, this works fine because, when the class was loaded(class loader thing), the CLASS_DATA_MEMBER data member, was created before all things happened, so thats why i dont get the error that i got from the DEFAULT_VALUE(instance data member) or from the first code i posted
is my assumption right? or is there any more better explanation with this?
Re: cannot referenced <instance data member> before super type constructr has been ca
Quote:
Originally Posted by
chronoz13
ok now, i think im getting to the point, just like what youve said, i tried using a static variable
Code :
public class NewClass {
private static int CLASS_DATA_MEMBER = 1;
public NewClass() {
this(CLASS_DATA_MEMBER);
}
public NewClass(int x){
}
}
what i notice, in my own opinion, this works fine because, when the class was loaded(class loader thing), the CLASS_DATA_MEMBER data member, was created before all things happened, so thats why i dont get the error that i got from the DEFAULT_VALUE(instance data member) or from the first code i posted
is my assumption right? or is there any more better explanation with this?
That's pretty much exactly how I look at it. That might not be technically true (depending on when you decide that an instance "exists"), but it makes sense to me.
Re: cannot referenced <instance data member> before super type constructr has been ca
Quote:
That might not be technically true
i dont mind, i know its way too complex and complicated (it might be clear to me, somewhere in future)
but
Quote:
but it makes sense to me.
im satisfied hearing this, atleast somehow, what i think or my assumptions makes sense
Re: cannot referenced <instance data member> before super type constructr has been ca
this could be the last thing i want to confirm
Quote:
So basically, the instance doesn't yet exist until that call to super() happens
- another thing i noticed and i forgot, the word Constructor, from the word itself, constructs an object(instance) together with its "instance" data members
(e.g MyClass objWithAllInstance = new MyClass()),
like what you have said, the instance doesn't yet exist until that call to super() happens, so THAT INVISIBLE SUPER CALL is the one responsible constructing an object of a class and its all instances
and in my case the DEFAULT_VALUE(instance data member) is not yet created, because the invisible super call is not yet being called
Re: cannot referenced <instance data member> before super type constructr has been ca
Yep! Maybe this will clear everything up:
Code java:
public class Main {
public Main() {
System.out.println("In super constructor.");
}
public static class InnerMain extends Main{
int i = getDefaultValue();
public int getDefaultValue(){
System.out.println("Getting default value.");
return 10;
}
public InnerMain(){
}
}
public static void main(String... args){
InnerMain im = new InnerMain();
}
}
What do you think that program prints? This is its output:
In super constructor.
Getting default value.
So, you can see that the super constructor happens before the initialization of the instance variables. And that's why you can't use the instance variables until the super constructor has been called.
Re: cannot referenced <instance data member> before super type constructr has been ca
seems like its really way to complicated, i think all these things should be enough for me right now, its not easy to take it all in, and im getting more questions the more i got answers, but THANK YOU SO MUCH FOR THE TIME, and thinking for a code for me to understand thank you so much kevin :)
Re: cannot referenced <instance data member> before super type constructr has been ca
and LOL, im mixing up some code on your code above, lol yeah i got it hahahahaha :D
Re: cannot referenced <instance data member> before super type constructr has been ca
No problem. Glad you got it figured out!
Re: cannot referenced <instance data member> before super type constructr has been ca
somehow yeah, thanks thanks :D