|
|
Re: forward reference rule
|
Posted: May 12, 2005 10:52 PM
|
|
It has nothing to do with initializing the variables.
It's about visibility. Normally a variable is only visible for the code that follows "under" the variable declaration.
Java makes one exception: class variables They are visible for every method in the class.
I don't know exactly when class variables are initialized, but I presume it happens before the constructor is called, because they are avaiable there (could also be, that they are initialized inside the constructor, but before the super call). But the initialization sequentially.
Therefore in your first example b does not yet exist.
In you second example the compiler first declares the 2 variables without executing the methode for chairsCount. Then it executes the method for chairsCount, after that it sets the value for tableCount. Try to print out the number of chairs inside initChairsCount(). You will see that it is 0, because tablesCount is still 0 at the time of execution.
Try the following: Instead of using int, use Integer. You will get a NullPointer Exception, because when the method gets executed, the variable inside does not have a value yet.
|
|