The answer is: according the JLF, superclass constructor is called
*before* any initialization statement in subclass. super() does not
actually run in aClass() but java compiler generates <init> method where
it is called before first initialization statement of aClass. The syntax
super(...) is allowed only to let class designer to point which
superclass constructor with which parameters he wants to use, not to use
super() as usual method. You can think of it another way: superclass
constructor cannot access subclass instance data members. Consider
making v static (then it will be initialized during class, not instance
initialization).

Hope this helps

All the Best
Pavel


Kontorotsui wrote:
> 
> I wonder why if I write this:
> 
> aClass extends aSuperClass
> {
>   aClass()
>   {
>      super(1);
>   }
> }
> 
> where the superclass is:
> 
> aSuperClass
> {
>   aSuperClass(int i)
>   {
>     ...
>   }
> }
> 
> anything works ok.
> 
> If instead I write
> 
> aClass extends aSuperClass
> {
>   final int v = 1;
> 
>   aClass()
>   {
>      super(v);
>   }
> }
> 
> with the same superclass, I get a "Can't reference v before the superclass
> constructor has been called." error from the compiler.
> 
> I understand that in general a variable cannot be referenced before the
> superclass constructor was called, but if that variable is a final, then it's a
> constant, why does the compiler complains?
> The two pieces of code are, semanthically, the same, aren't they?
> The javac pre-processor could even substitute the constant values before
> compiling!
> 
> It's the java language that is excessively pedantic or there is something I'm
> missing?
...


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to