The Artima Developer Community
Sponsored Link

Computing Thoughts
Generics and Packages
by Bruce Eckel
November 10, 2005
Summary
There appear to be some strange side effects on packages when using generics.

Advertisement

Consider the following file:

package code;

interface HasColor {
  java.awt.Color getColor();
}

class Dimension {
  int x, y, z;
}

class ColoredDimension<T extends Dimension & HasColor> {
  T item;
  ColoredDimension(T item) { this.item = item; }
  T getItem() { return item; }
  java.awt.Color f() { return item.getColor(); }
  int getX() { return item.x; }
}

When you run the compiler, you get the following error message:

GenericsAndPackages.java:17: x is not public in code.Dimension; cannot be accessed from outside package
  int getX() { return item.x; }
                          ^
1 error

Since all the code is in the same package, this is probably a bug. But with generics, it's always hard to tell.

Talk Back!

Have an opinion? Readers have already posted 14 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Bruce Eckel adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Bruce Eckel (www.BruceEckel.com) provides development assistance in Python with user interfaces in Flex. He is the author of Thinking in Java (Prentice-Hall, 1998, 2nd Edition, 2000, 3rd Edition, 2003, 4th Edition, 2005), the Hands-On Java Seminar CD ROM (available on the Web site), Thinking in C++ (PH 1995; 2nd edition 2000, Volume 2 with Chuck Allison, 2003), C++ Inside & Out (Osborne/McGraw-Hill 1993), among others. He's given hundreds of presentations throughout the world, published over 150 articles in numerous magazines, was a founding member of the ANSI/ISO C++ committee and speaks regularly at conferences.

This weblog entry is Copyright © 2005 Bruce Eckel. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us