OK. Can someone please explain this to me. I'm getting the compile
error:

> Class x not found in type declaration.


This occurs when I have one package that defines a public interface and
another package which implements this interface with a private class of
the same name.

For example:

file a/x.java:
> package a;
> public interface x { public void dummy(); }

file b/x.java:
> package b;
> class x implements a.x { public void dummy() { return; } }

file b/y.java
> package b;
> public class y { public x newX () { return new x(); }


I use both packages as follows:

file z.java:
> import a.*;
> import b.*;
> class z {
>   public static void main(String argv[]) {
>     y iy = new y();
>     x ix = y.newX();
>   }
> }

Trying to compile z.java gives the error:
> Class x not found in type declaration.

I have found two ways to prevent the compile error:
(1) explicitly import the interface:
    > import a.x;
(2) qualify the declaration of ix with the package name:
    >     a.x ix = y.newX();


I *really* want to know why this happens. It seems *obvious* to me that
the z.java file should only see the interface a.x, since the class b.x
is not exported from the package b, and therefore there should be no
confusion or namespace collision. If class b.x was *public*, then I
would expect a compile error, and I get one. It's the same as before:
"Class <x> not found in type declaration." It is not very informative
e.g. it could say "ambiguous type reference" or something similar.


You might say "so change the name of the private class in the package b
to something else, or qualify the offending bits", but this doesn't help
me becuase I'm trying to use someone else's software; namely the RST
JDBC drivers. Although there isn't a large volume of code, I don't want
to go through and change things without understanding the problem.

The RST JDBC drivers have (among others) a private class
rst.sql.Connection, which implements java.sql.Connection. This is
returned by the DriverManager.getConnection() method, via the public
class rst.sql.Driver (which implements java.sql.Driver). So we have a
piece of code which reads:

> Connection con = DriverManager.GetConnection(argv[0], argv[1], argv[2]);
> Statement stmt = con.createStatement();
> ...and so on...

This could be fixed by:

> java.sql.Connection con = DriverManager.GetConnection(argv[0], argv[1], argv[2]);


Cheers.

-- 
Some groovy quote goes here....
A CD worth hearing: Deep Dish - Junk Science

Reply via email to