The "super" reference

Posted on 2013-12-04
Last Modified: 2013-12-05
I am trying to utilize inheritance to use variables from a parent class in a child class. So that I don't have to repeat everything, I am simply using the " super(parameters) ; " statement. But when I do this I keep getting an error that says, "error: can not reference variable before supertype constructor has been called."

I don't know what this is telling me to do.

I'm using jgrasp writing java.
Question by:aminkeith
LVL 27

Assisted Solution

dpearson earned 100 total points
ID: 39697425
Can you post the code that is having this problem?

If I had to guess it sounds like you might have:

public class MyBase {
    protected int value ;

    public MyBase(int theValue) {
        value  =theValue ;

public class Child extends MyBase {
    public Child(int theValue) {
       super(value) ;  // Accidentally using base class's variable

But if you post the code it'll be easier to figure out :)

LVL 12

Accepted Solution

Gibu George earned 100 total points
ID: 39697479
If you want to use the instance variables of the super class in child class, you need to make those instance variables protected instead of private, then in the first line of code in the constructor of the child class should the super() call, with super class constructor parameters
LVL 37

Assisted Solution

zzynx earned 200 total points
ID: 39697634
>> can not reference variable before supertype constructor has been called
The first thing to do in a method you override is calling super()
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

LVL 16

Assisted Solution

Valeri earned 100 total points
ID: 39697671
it means that you are trying to use variable (the one that is mentioned in the error message) that is still not created, because the object that this variable is part of is still not created.
When you invoke "super(parameters);" probably one of your parameters is member of the child class or base class, both of them are still not created.
Also keep in mind that "super" should be the first line in the constructor of the child classes.

Shoud be something like that:

public class Base {
    protected int valueBase ;

    public Base(int theValue) {
        valueBase = theValue ;

public class Child extends Base {
    private int valueChild;

    public Child(int theValue) {
       super(theValue) ; // creates the base class first
       valueChild = theValue;      
LVL 37

Assisted Solution

zzynx earned 200 total points
ID: 39697678
>> The first thing to do in a method you override is calling super()
That should have been:
The first thing to do in a constructor you override is calling super()

Author Comment

ID: 39700135
Thanks all of you guys. I actually figured out what my problem was. I think I had them set to private instead of protected. Thanks anyway.
LVL 37

Expert Comment

ID: 39700337
You're welcome.
Thanx 4 axxepting

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql import cannot be resolved jsp 3 48
printf performancy 11 71
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 30
collection output issue 9 37
If you haven’t already, I encourage you to read the first article ( in my series to gain a basic foundation of R and R Studio.  You will also find the …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question