issue with inherited static members in java interfaces (possible bug?)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

issue with inherited static members in java interfaces (possible bug?)

Jason Kinzer
Hi,

Noticed something a bit odd (am using Scala2), here is a minimal test case:

// I1.java
package test;

public interface I1 {
    public static final int i = 1;
}

// I2.java
package test;

public interface I2 extends I1 {
    public static final String s = "foo";
}

// test.scala
object testi {
  import test._;
  import test.I2._;

  def main(args: Array[String]): Unit = {
    I1.i; // ok
    I2.s // ok
    I2.i; // nope. compile-time error: 'value i is not a member of test.I2'

    s; // ok
    i; // still no luck: ' error: not found: value i'
  }
}

Is this a bug or intended behavior?

Thanks,
Jason
Reply | Threaded
Open this post in threaded view
|

Re: issue with inherited static members in java interfaces (possible bug?)

Adriaan Moors-2

On 10 Feb 2006, at 17:12, Jason Kinzer wrote:

Hi,

Noticed something a bit odd (am using Scala2), here is a minimal test case:

<snip: code that tries to access an inherited static member>

Is this a bug or intended behavior?
As far as I know, this is consistent with how it works in Java: static members aren't inherited. So I guess that it's intended to work this way.

However, IMO, this is a pity: I think Smalltalk's 'compatibility metaclass'-approach (or better yet,
the ObjVLisp approach, where you can also explicitly specify metaclasses that are specific to one class) is far more elegant 
(although not straightforward to implement in a JVM environment, I guess).

(Now we're on the subject of metaclasses, let me plug one of my favourite papers of 2005:
  A Core Calculus of Metaclasses by Sam Tobin-Hochstadt and Eric Allen.

regards,
adriaan
Reply | Threaded
Open this post in threaded view
|

Re: issue with inherited static members in java interfaces (possible bug?)

Jason Kinzer
On 2/10/06, Adriaan Moors <[hidden email]> wrote:

As far as I know, this is consistent with how it works in Java: static members aren't inherited. So I guess that it's intended to work this way.


Hmmmm... empirically this does not appear to be the case:

// I1.java
package test;

public interface I1 {
    public static final int i = 1;
}

// I2.java
package test;

public interface I2 extends I1 {
    public static final String s = "foo";
}

// Test.java

import test.*;

class Test
{
    public static void main(String[] args) {
        System.out.println(I2.i); // ok
        System.out.println(I2.s); // ok
    }
}

Thanks,
Jason
Reply | Threaded
Open this post in threaded view
|

Re: issue with inherited static members in java interfaces (possible bug?)

Adriaan Moors-2

On 10 Feb 2006, at 19:02, Jason Kinzer wrote:

On 2/10/06, Adriaan Moors <[hidden email]> wrote:

As far as I know, this is consistent with how it works in Java: static members aren't inherited. So I guess that it's intended to work this way.


Hmmmm... empirically this does not appear to be the case:
I stand corrected, but I wasn't completely wrong (I think) ;-)

According to http://www.artima.com/designtechniques/static.html, static members aren't dynamically bound (they are implicitly final), so, although they are inherited, they can't be overridden -- only hidden... this makes them quite useless (for anything else than simple utility methods)

sorry about the mistake, 
adriaan
Reply | Threaded
Open this post in threaded view
|

Re: issue with inherited static members in java interfaces (possible bug?)

Nikolay Mihaylov
On Fri, 2006-02-10 at 21:36 +0100, Adriaan Moors wrote:

> According to http://www.artima.com/designtechniques/static.html,
> static members aren't dynamically bound (they are implicitly final),
> so, although they are inherited, they can't be overridden -- only
> hidden... this makes them quite useless (for anything else than simple
> utility methods)

Whatever the case, they are there and we have to live with that. I think
static members should always be accessed via the class where they are
defined and in this regard I consider Java an aberration. Luckily, Scala
agrees with me :)

I thought the issue was already discussed on the mailing list but maybe
I'm wrong because I couldn't find it with gmane. To avoid future
confusion, I went to the Scala Wiki page and added some information on
how the Scala compiler treats static members from Java classes (and I
stole the Java example from Jason's mail):

http://scala.sygneca.com/faqs/interoperability

Kudos to Jamie Webb for organizing the Wiki

Nikolay