Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I refer to an instance method while explicitly invoking a constructor?

Does anyone know why you can reference a static method in the first line of the constructor using this() or super(), but not a non-static method?

Consider the following working:

public class TestWorking{
    private A a = null;
    public TestWorking(A aParam){
       this.a = aParam;
    }

    public TestWorking(B bParam)
    {
        this(TestWorking.getAFromB(bParam));
    }

    //It works because its marked static.
    private static A getAFromB(B param){
        A a = new A();
        a.setName(param.getName());
        return a;
    }
}

And the following Non-Working example:

public class TestNotWorking{
    private A a = null;
    public TestNotWorking(A aParam){
       this.a = aParam;
    }

    public TestNotWorking(B bParam)
    {
        this(this.getAFromB(bParam));
    }

    //This does not work. WHY???
    private A getAFromB(B param){
        A a = new A();
        a.setName(param.getName());
        return a;
    }
}
like image 344
Koekiebox Avatar asked Sep 10 '10 14:09

Koekiebox


People also ask

Can the constructor call instance methods?

Yes, as mentioned we can call all the members of a class (methods, variables, and constructors) from instance methods or, constructors.

Can an instance method be called on an object?

You can not call an instance method in the static method directly, so the Instance method can be invoked using an object of the class.

What does it mean to invoke a constructor?

The invocation of a constructor constructs a new object of the class to which the constructor belong and returns a reference to the created object. The object is constructed by making use of the parameters passed to the constructor. Example: new String("test")


4 Answers

Non-static methods are instance methods. This are only accessible in existing instance, and instance does not exist yet when you are in constructor (it is still under construction).

Why it is so? Because instance methods can access instance (non-static) fields, which can have different values in different instances, so it doesn't make sense to call such method on something else than existing, finished instance.

like image 71
amorfis Avatar answered Oct 08 '22 21:10

amorfis


See the Java Language Specification 8.8.7.1. This states that

An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods or inner classes declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.

This is because you can not call an instance method before the instance is created. By the way, it is possible to call an instance method later on in the constructor (although not a solution for you).

like image 24
Marc Avatar answered Oct 08 '22 19:10

Marc


I think its's because final instance variables are not set yet (so you have no instance yet) and an instance method could access one. Whereas all static initialization has been done before the constructor call.

Greetz, GHad

like image 1
GHad Avatar answered Oct 08 '22 20:10

GHad


because when you calling this or super in constructor your object is not constructed yet. (your instance is not initialized completely yet). so calling an instance method doesn't make scene.

like image 1
mhshams Avatar answered Oct 08 '22 21:10

mhshams