A couple of questions
-
If I declare a variable in a class, class A, as
private int blah = myclass.func();When is the func() actually called? The first time class A is initiated?
-
If I have
public final int blah = myclass.func();
I know that somehow blah is dynamically updated (e.g. everytime I call some other function in class A, I always get an updated value of blah – which was obtained by calling myclass.func())… but I don’t know why. Would someone explain this behavior?
Thanks,
Mike
You’re acting like
blahisstatic.Each time class A is instantiated
There exists one
blahfor every instance (object) of class A. The methodfunc()will be called and its value assigned to each new instance ofblahevery time you saynew A(). Each time it appears thatblahis updated, it’s because you’re looking at a completely different object than last time.