Compile error accessing method in an adjacent package

R

Rhino

I'm an experienced Java programmer but I've been away from Java for a few
years and need a bit of a memory assist. Also, I don't remember all of the
terminology so forgive me if I use the wrong word occasionally: I'll try to
be clear in what I mean so that you understand what I'm asking.

I have two packages in the same Eclipse (Galileo) project: com.foo.old and
com.foo.new. The "new" package does the same thing as the "old" package but
has a couple of new classes that have some new functionality. All of the
existing classes in the "old" project are completely unchanged. The "new"
package contains only the classes containing the new functionality and a new
version of the "driver" program from the old package. The "driver" program
basically just instantiates each of the other classes in turn with
statements like this, where Alpha is the class called by the driver program
and writeBlah() is its primary method, and x and y are parameters passed
from the driver program (x is a String and y is an Object):

new Alpha().writeBlah(x, y);

In the "old" package, the driver program finds classes Alpha, Beta and all
of their peers in its own package. The old version of the driver program
works perfectly.

My problem is in accessing the classes in the old package from the "new"
version of the driver in the "new" package.

I did CTRL-SHIFT-O in Eclipse so that it would find Alpha and its peers in
the "old" package and the statement to import com.foo.old appeared without
difficulty. However, now I get a compile error. The method name, writeBlah,
is underlined in red and the message says "the method writeBlah(String,
Object) in the type Alpha is not applicable for the arguments (String,
Object).".

I don't understand why I'm getting this message. I am passing the correct
parameters to the method and they are in the correct order. In fact, I
haven't changed the content of the code in Alpha at all. The only new thing
is that the new version of the driver program is accessing Alpha in a
different package. I thought perhaps I had a scope problem but all of the
methods in Alpha are public.

Can someone enlighten me on what silly thing I've done and how to fix this?

Just to recap then, com.foo.old contains:
MyDriver.java (old version), which instantiates Alpha, Beta, and Gamma
Alpha.java
Beta.java
Gamma.java

com.foo.new contains:
MyDriver.java (new version), which instantiates Alpha, Beta, and Gamma from
com.foo.old and Delta from com.foo.new
Delta.java

By the way, if this approach is wrong and there is a better one, I'd be glad
to hear about it. I'm not wedded to the idea of doing this via an "old" and
a "new" package.
 
M

Mike Schilling

Rhino said:
I'm an experienced Java programmer but I've been away from Java for a
few years and need a bit of a memory assist. Also, I don't remember
all of the terminology so forgive me if I use the wrong word
occasionally: I'll try to be clear in what I mean so that you
understand what I'm asking.
I have two packages in the same Eclipse (Galileo) project:
com.foo.old and com.foo.new. The "new" package does the same thing as
the "old" package but has a couple of new classes that have some new
functionality. All of the existing classes in the "old" project are
completely unchanged. The "new" package contains only the classes
containing the new functionality and a new version of the "driver"
program from the old package. The "driver" program basically just
instantiates each of the other classes in turn with statements like
this, where Alpha is the class called by the driver program and
writeBlah() is its primary method, and x and y are parameters passed
from the driver program (x is a String and y is an Object):
new Alpha().writeBlah(x, y);

In the "old" package, the driver program finds classes Alpha, Beta
and all of their peers in its own package. The old version of the
driver program works perfectly.

My problem is in accessing the classes in the old package from the
"new" version of the driver in the "new" package.

I did CTRL-SHIFT-O in Eclipse so that it would find Alpha and its
peers in the "old" package and the statement to import com.foo.old
appeared without difficulty. However, now I get a compile error. The
method name, writeBlah, is underlined in red and the message says
"the method writeBlah(String, Object) in the type Alpha is not
applicable for the arguments (String, Object).".

If writeBlah isn't declared public, it won't be callable from another
package.
 
R

Rhino

Mike Schilling said:
If writeBlah isn't declared public, it won't be callable from another
package.

writeBlah is declared public. That's the first thing I checked.

I've managed to resolve the error but I'm not quite sure why my solution
worked.

My new package was initially just a clone of the old package and contained
all of the same classes. When I realized that I was duplicating a great deal
of source code unnecessarily, I started deleting the classes that hadn't
changed, one at a time. So I deleted Alpha and got the message I mentioned.

A few minutes ago, just for the heck of it, I deleted all of the classes
that hadn't changed from the new package and did my CTRL-SHIFT-O to access
them from the old package. Immediately after that, I had no more error
messages. I had fully expected to get error messages for each and every one
of the classes that I had deleted but the exact opposite happened and I have
no errors at all any more. Alpha, Beta and Gamma all extend abstract class
Baz and implement interface Fuzz. My best guess is that there was some kind
of dependency between Alpha, Beta, Gamma, Baz and Fuzz that disappeared when
all the duplicated classes were being accessed from the same package.
Therefore, when Alpha was being accessed from the old package but Baz and
Fuzz were being accessed from the new package, they wouldn't play nicely but
when they were all being accessed from the old package, they play together
very well.

Does that make sense?
 
L

Lew

Rhino said:
I have two packages in the same Eclipse (Galileo) project: com.foo.old and
com.foo.new. The "new" package does the same thing as the "old" package but
has a couple of new classes that have some new functionality. All of the
existing classes in the "old" project are completely unchanged. The "new"
package contains only the classes containing the new functionality and a new
version of the "driver" program from the old package. The "driver" program
basically just instantiates each of the other classes in turn with
statements like this, where Alpha is the class called by the driver program
and writeBlah() is its primary method, and x and y are parameters passed
from the driver program (x is a String and y is an Object):

new Alpha().writeBlah(x, y);

In the "old" package, the driver program finds classes Alpha, Beta and all
of their peers in its own package. The old version of the driver program
works perfectly.

My problem is in accessing the classes in the old package from the "new"
version of the driver in the "new" package.

I did CTRL-SHIFT-O in Eclipse so that it would find Alpha and its peers in
the "old" package and the statement to import com.foo.old appeared without
difficulty. However, now I get a compile error. The method name, writeBlah,
is underlined in red and the message says "the method writeBlah(String,
Object) in the type Alpha is not applicable for the arguments (String,
Object).".

I don't understand why I'm getting this message. I am passing the correct
parameters to the method and they are in the correct order. In fact, I
haven't changed the content of the code in Alpha at all. The only new thing
is that the new version of the driver program is accessing Alpha in a
different package. I thought perhaps I had a scope problem but all of the
methods in Alpha are public.

Please provide an SSCCE that evinces the problem.
<http://sscce.org/>

Based on the heavily paraphrased, incomplete and redacted information you
provide, your problem should not exist.
 
M

markspace

Rhino said:
My best guess is that there was some kind
of dependency between Alpha, Beta, Gamma, Baz and Fuzz that disappeared when
all the duplicated classes were being accessed from the same package.


Well, your basic problem is that you've got a lot of classes with the
same name, and you're not being careful about whether they come from
package A or package B. And it's unlikely that the IDE is going to be
able to correctly guess.

I think you should get rid of the import statements and use fully
qualified names, like this:

com.foo.a.Alpha alph = new com.foo.a.Alpha();
com.foo.b.Baz baz = new com.foo.b.Baz();

It'll make your code much more clear. The real danger here is that
you'll finish this code, put it away and then six months from now you'll
have to modify it again, and you'll have NO idea what you did. The code
will be completely unmaintainable.

BTW, I switch to package names "a" and "b" because "new" is a keyword
and you can't have a package named "new." Just saying...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top