1

I have something like this:

class A {
    final int f = 1;
    int getF() {
        return f;
    }
}

class B extends A {}

Is it possible to call getF() in B class, and get 0 from it? Can this final field has another value from 1? Cause I get 0 result from getF() sometimes and have no idea what happens. No overrides or something. Just calling getF() and getting 0. In some situations. Can somebody guess why can it be like this?

user1748526
  • 464
  • 5
  • 18

4 Answers4

3

The only combination of circumstances that I can think would allow this would be if there is another class above A that declares a method which is called by its constructor, and B overrides that method and calls getF from within there

class Super {
  public Super() {
    doInit();
  }

  protected void doInit() { /* ... */ }
}

class A extends Super {
  // f and getF as before
}

class B extends A {
  protected void doInit() {
    System.out.println(getF());
  }
}

Or similarly if the constructor "leaks" a this reference to somewhere where it can be accessed before the constructor has finished running, thus breaking the usual immutability guarantees of finals.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
1

With only the code as given, no, it's not possible to get anything but 1 from getF(). I suspect there is more to the story. For example, if you override getF(), either in B or a subclass of B, you could get different results.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • 1
    The OP does state that there are "no overrides" – NullUserException Dec 10 '12 at 21:36
  • @NullUserException Right, but I don't believe the OP's assertion. – erickson Dec 10 '12 at 22:18
  • proxy classes and reflection could both modify or appear to modify the value. You can also get the debugger to tell java that 1 is actually 0. – BevynQ Dec 11 '12 at 07:48
  • Sure, but it's much more likely that there's a perfectly mundane explanation that the poster has overlooked. In my experience, it's most efficient to rule out human error first, *then* start looking for an exotic technical explanation. – erickson Dec 11 '12 at 18:35
0

Is this what you are looking for??

public class A {

final int f = 1;

int getF() {
    return f;
 }
}

public class B extends A {
public int getF() {
    return f;
}

public void setF(int f) {
    this.f = f;
}

int f;
public B(){
 f=0;
}
public static void main(String args[]) {
    B a = new B();
    System.out.println(a.getF());
  }
}
NullUserException
  • 83,810
  • 28
  • 209
  • 234
sathish_at_madison
  • 823
  • 11
  • 34
0

There is not legitimate way for 0 to be returned, from the information you have provided. However there are semi-legitimate and other hacky ways for it to happen. What you will need to do to diagnose this problem is to put conditional stop points on the code where you see the 0 being returned. I suspect the structure that is present is not the one you expect, the debugger should show you the structure and why the value is 0.

BevynQ
  • 8,089
  • 4
  • 25
  • 37