Author | Topic: forward referencing |
rahul_mkar greenhorn |
posted May 12, 2000 12:10 AM
can anybody explain this. public class AQuestion Answers Compiler error complaining about access restriction of private variables of AQuestion.
|
threadscheduler greenhorn |
posted May 12, 2000 01:42 AM
The comiler complains about a Forward Referencing error and not about the access protection. This is because JAVAC is a single pass compiler. The moment it comes across the statement int i=j; it flags an compile time error. so further checks on the statement are not done till the present error is removed. Now if we remove the forward referencing : private access modifier restricts access from other classes but we are using the variable i in the same class AQuestion there is now no compile time error.
|
rahul_mkar greenhorn |
posted May 12, 2000 11:41 PM
what is the difference between my question above and the question below and why do they give different results.
private int giveMeJ() public static void main(String args[]) Answers 1. Compiler error complaining about access restriction of private variables of AQuestion. the answer is 3 for above
[This message has been edited by rahul_mkar (edited May 13, 2000).]
|
Prabhu greenhorn |
posted May 13, 2000 07:48 AM
Rahul, IMO when you are declaring int i in the second program, you're pointing this to get the value from the result of a method which is OK. During compile time, it just checks whether that method with appropriate signature is coded or not (Anywhere in the program, not necessarily before the first reference). Since it's coded somewhere(giveMeJ()), it compiles fine. During runtime, it tries to execute the method and since it could find one to execute, it runs fine and gets the value. HTH
|
maha anna bartender |
posted May 13, 2000 09:21 AM
rahul_mkar, Prabhu is correct. To add on to this, at runtime when an object of this class is created, since instance vars are initialized in the SAME SEQUENCE as they are written in the source file, the var i is initialized first, which in turn calls the giveMeJ() fn which in turn returns the instance var j. The important point to note here is when giveMeJ() is called, the instance var j is NOT YET initialized and so all the instance vars have their default values, the default val for 'int' type which is 0 (zero) is returned. If the ORDER of initialization WOULD have been changed like the foll. then the var i will have value 10. regds
|
rahul_mkar greenhorn |
posted May 13, 2000 11:19 PM
hi, i would like to know 1) what is the sequence of variable creation, is it static variables first and then instance variables or vice versa } both at almost the same time;
"private int i = j; private int j = 10; comiler complains about a Forward Referencing error" in the above case variables i and j should have been initialized to 0 and then when it comes to the first statement ie "private int i=j" then i should have been assigned 0 and then in the next statement it should have been assigned 10. However this does not happen.
|
maha anna bartender |
posted May 14, 2000 04:05 AM
rahul_mkar, for your first request static init is done first (when the class is loaded into memory itself), then comes the instance init (done when an object is created from this class). At the same time when you do new ThisClass() then if the class is not already in memory, then it is loaded and both static and instance inits are done. Otherwise if it was already loaded before then only the instance init are done and the correcponding constructor is called. So in your code the static var i is initilized first and then z is initialized. I wrote this sample prog. for you. Play with this. It WILL DEFINITELY make you think./confuse/....clear at the end ************************************************************
********************************************************* ORDER OF EXECUTION 1. - static initialization for test1 done...
********************************************************* for your 2nd request regds
Please refer to JLS especially this section regds [This message has been edited by maha anna (edited May 14, 2000).]
|
prpanicker@yahoo.com unregistered |
posted May 14, 2000 05:47 AM
quote:
|
| | |