Home arrow static arrow Java Programming [Archive] - Why does Java allow this?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Why does Java allow this?
This topic has 1 reply on 1 page.

Posts:61
Registered: 12/27/03
Why does Java allow this?  
Jul 5, 2004 7:34 AM



 
Hello Folks

Today I saw the following code in an exam about Java programming:

interface I {	public final static double var = 32.0;} class A implements I {	double var = 15;} 


How can Java allow such a construct? I overload a variable that is public, static and final and replace it with an instance variable that is protected. In my opinion the compiler should reject that, I mean it does it when I try the same with a method. Why is it allowed with variables?

Regards
Der Hinterwaeldler
 

Posts:11,200
Registered: 7/22/99
Re: Why does Java allow this?  
Jul 5, 2004 7:58 AM (reply 1 of 1)



 
The declaration of the variable var in class A does not override the declaration in the interface I, it only hides it.

Methods are different because methods can be overridden. Static and final methods are an exception, they can't be overridden so trying to do it is a compile time error.

See also http://java.sun.com/developer/TechTips/2000/tt1010.html#tip2
 
This topic has 1 reply on 1 page.