Originally posted by sonir shah:
What will be the output when class Test is run:
Options:
1)It will print 'In Sub' and 'QBANK'
2)It will print "QBANK"
3)Depends on the implementation of the JVM
4)It will not even compile
5)None of the above
Answer : 2)It will print "QBANK"
I feel that when in the main Sub.Id is called it should first print "In Sub" and then "QBANK" i.e the 1) option.
Where am I wrong?
Sonir
Rob
SCJP 1.4
Originally posted by sonir shah:
What will be the output when class Test is run:
Options:
1)It will print 'In Sub' and 'QBANK'
2)It will print "QBANK"
3)Depends on the implementation of the JVM
4)It will not even compile
5)None of the above
Answer : 2)It will print "QBANK"
I feel that when in the main Sub.Id is called it should first print "In Sub" and then "QBANK" i.e the 1) option.
Where am I wrong?
Sonir
Originally posted by Maulin, Vasavada:
hi sonir,
again, classes r not loaded when u access its "static" members. classes r loaded when u create an instance of it.
thats why the class B is not loaded and u get direct value of var inherited from A.
regards
maulin.
Rob
SCJP 1.4
Originally posted by Rob Ross:
The compiler knows, at compile time, in which class a particular declaration can be found. At compile time, the compiler looks at Sub, and since there is no definition for the member 'i', it then looks in the class Super, and finds it there; now it can write the bytecode for the println statement at compile time. There is no run-time checking necessary for this, because you are using static references. As you mentioned above, if you actually try to instantiate a class, and then reference the member 'i' via that class instance, then the *run time* system has to get the current value of i.
Rob
Originally posted by Lubosh Bazant:
I tried to run this excercise, however I always get the error that a non-ststic variable cannot be referenced from a static context. The variable in the super class is declared as static, exactly as in the first submission. Can anyone explain?
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh