Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static method called from constructor in Java

Question about Java static methods.

Animal() 
{ 
    this(makeRandomName());          
}

I have this code in Java, that is called when I create an animal object: Animal a = new Animal() makeRandomName is a method that returns a String obtained randomly from an Array of String values, by using Math.random(). If I don't specify the method makeRandomName as static, I get this error (can you explain why):

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot reference this before supertype constructor has been called

Also, when the Animal constructor is defined like this:

Animal() 
{
    this.name = makeRandomName();           
}

I don't get any errors, regardless of whether makeRandomName is static or non-static. Why? What is the difference between this.name = makeRandomName(); and this(makeRandomName());

I have never seen this syntax this(method_name()) before, I have only ever seen this.instance_variable = value, so I am a bit confused. I am sure this has to do with super constructors and order of invoked methods, but it would be great to see an expert analysis of methods and constructors in this case and order of invoking methods. Many thanks in advance!

I was asked to post the entire code:

public class Animal {
    String name;
    Animal (String n)
    {
        this.name = n;
    }
    Animal() 
    {

        this(makeRandomName());
        //this.name=makeRandomName();

    }
    static String makeRandomName()
    {
        int x = (int) (Math.random()*5);
        String l[] = new String[] {"Zlatan", "Ibra", "Edinson", "Gigi", "T"};
        return l[x];

    }
    public static void main(String [] args)
    {
        Animal a = new Animal();
        Animal b = new Animal("M");
        System.out.println(a.name);
        System.out.println(b.name);
    }
}
like image 816
Samy Avatar asked Jun 10 '26 15:06

Samy


1 Answers

For the first question - the reason makeRandomName() must be specified as static is because makeRandomName() is an instance method and will not be accessible until super has been called from within the constructor, which causes the class to be initialzed. What you are attempting to do is call makeRandomName() before the call to super initializes the class, thereby resulting in the compilation error.

For the second question - there is an implicit call to super(); before your statment of this.name = makeRandomName();. super(...); must always be the first statement in the constructor, even if you don't write it explicitly. Therefore, the actual code of your second constructor is this:

Animal() {
    super();
    this.name = makeRandomName();
}

makeRandomName() is accessible at this point even if it is declared as non-static because the object has been constructed already.

like image 63
Oli Avatar answered Jun 12 '26 06:06

Oli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!