Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Heres some sample code,

class Base
{
  private int val;

  Base() {
  val = lookup();
  }

  public int lookup() {
    //Perform some lookup
  // int num = someLookup();
  return 5;
  }

  public int value() {
  return val;
  }
}

class Derived extends Base
{
  private int num = 10;

  public int lookup() {
  return num;
  }
}


class Test
{
  public static void main(String args[]) {

  Derived d = new Derived();
  System.out.println("d.value() returns " + d.value());

  }
}

output: d.value() returns 0 // I expected 10 as lookup() is overridden, but not 0! can someone clarify this?

The initialization of Derived's instance variables has not happened at the time its lookup method executes. How do I make sure the instance variables of Derived are initialized when its method is called?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
116 views
Welcome To Ask or Share your Answers For Others

1 Answer

Well for a start, that code doesn't compile due to the lack of someLookup method.

Anyway, asides from that I believe your issue is that your expections are invalid because of the way constructors are run hierarchically.

A superclass' constructor is always run before the subclass', and this includes initializers for the subclass' variables (which are really run as part of the constructor). So, when you create your instance of Derived, the following happens:

  1. The Base constructor is invoked first.
  2. lookup() is called, which uses the implementation in Derived.
  3. num is returned, which is the default value at this point because Derived's constructor and initializers have not been run.
  4. val is set to 0.
  5. The Derived initializers and constructor are run - calling lookup from this point on will return 10.

In general, it's a bad idea to call a non-final method from a constructor for exactly this reason, and many static analysis tools will warn you against it. It's similar to letting object references leak during construction, you can end up with an instance that invalidates class-level invariants (in your case, Derived's num is "always" 10 yet it can be seen to be 0 at some points).

Edit: Note that for this particular case, without any additional code, you could resolve the issue by making num a constant:

class Derived extends Base
{
  private static final int num = 10;
  ...

This would actually do what you want, because the static initializer is run when the class is loaded (which has to happen before the constructors are called). This does however assume that it's fine for:

a) all instances of the class to share the same num variable; b) num never needs to change (if this is true then (a) is true automatically).

In the exact code you've given this is clearly the case, but I expect you may be omitting extra functionality for brevity.

I include this here for comparison and interest, not because it's a workaround to this "issue" in a general sense (because it's not).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...