Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: cannot referenced <instance data member> before super type constructr has been called

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default cannot referenced <instance data member> before super type constructr has been called

    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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default 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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: cannot referenced <instance data member> before super type constructr has been ca

    wow thats it!,

    questions:
    1.)
    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

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: cannot referenced <instance data member> before super type constructr has been ca

    Quote Originally Posted by chronoz13 View Post
    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:
    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 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default 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
    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?
    Last edited by chronoz13; August 8th, 2011 at 10:36 AM.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: cannot referenced <instance data member> before super type constructr has been ca

    Quote Originally Posted by chronoz13 View Post
    ok now, i think im getting to the point, just like what youve said, i tried using a static variable
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: cannot referenced <instance data member> before super type constructr has been ca

    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
    but it makes sense to me.
    im satisfied hearing this, atleast somehow, what i think or my assumptions makes sense

  10. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: cannot referenced <instance data member> before super type constructr has been ca

    this could be the last thing i want to confirm
    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

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: cannot referenced <instance data member> before super type constructr has been ca

    Yep! Maybe this will clear everything up:

    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. The Following User Says Thank You to KevinWorkman For This Useful Post:

    chronoz13 (August 8th, 2011)

  13. #12
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default 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

  14. #13
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default 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

  15. #14
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: cannot referenced <instance data member> before super type constructr has been ca

    No problem. Glad you got it figured out!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #15
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: cannot referenced <instance data member> before super type constructr has been ca

    somehow yeah, thanks thanks

Similar Threads

  1. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM
  2. Need something like a "Hashtable<String[], Integer>" kind of data type @@
    By Albretch Mueller in forum Java Theory & Questions
    Replies: 2
    Last Post: November 27th, 2010, 10:24 PM
  3. About heap data type
    By PaddyWangTheGG in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 17th, 2010, 03:02 PM
  4. Selection Sorting of Data type (Char)
    By chronoz13 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:28 PM
  5. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 08:19 AM