p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java thread: RE: Question about packages


Message #1 by Jonathan Gold <jonnygee@p...> on Thu, 18 Apr 2002 12:10:29 -0700
This is great. Thank you for putting up with my posts. I didn't realize that
the constructors had to be made PUBLIC as well! I'm down to one error: the
one concerning not recognizing one of my method calls. I'm still stumped. I
repeat it from the last post:

I have my package now: it contains three classes: mcmLength, tkgWeight, and
my Carpet class. All are declared as public.

I have a class called TestCarpet that uses these three to compute the areas
and total weights of various carpets. Before I put the three classes into a
package this TestCarpet class worked fine.

Then I put the three into a package called Measures and I added an "import
Measures.*;" statement to the TestCarpet class. I get this error now:

I have many methods in the class tkgWeight, and one of them is an accessor
that returns the weight in kilograms as a double. It is declared as "public
double getWeightInKilos()". The error simply says that it cannot resolve the
symbol getWeightInKilos(). This error did not occur when the classes existed
outside the package. Why should it occur now?

Thanks for any help,

Jonathan

Jonathan Gold
jonnygee@p...

Message #2 by Jonathan Gold <jonnygee@p...> on Thu, 18 Apr 2002 10:44:42 -0700
I solved part of the problem: even though the classes are in the same
package, I still needed to add a classpath to the compiler call. OK.

Now I have a different trouble: I have my package now: it contains three
classes: mcmLength, tkgWeight, and my Carpet class. All are declared as
public.

I have a class called TestCarpet that uses these three to compute the areas
and total weights of various carpets. Before I put the three classes into a
package this TestCarpet class worked fine.

Then I put the three into a package called Measures and I added an "import
Measures.*;" statement to the TestCarpet class. I get three errors now:

Two of the errors claim that "mcmLength(int, int, int) is not public in
Measures.mcmLength; cannot be accessed from outside package". I assure you
that I have declared mcmLength as "public class mcmLength". Furthermore, I
have declared tkgWeight the same way (as "public class tkgWeight") and I am
not getting any errors directly concerned with that. Any ideas? Should I
post my code?

The third error baffles me as well. Remember, none of these errors occurred
when the classes existed outside the package. I have many methods in the
class tkgWeight, and one of them is an accessor that returns the weight in
kilograms as a double. It is declared as "public double getWeightInKilos()".
The error simply says that it cannot resolve the symbol getWeightInKilos() .

Thanks for any help,

Jonathan

Jonathan Gold
jonnygee@p...


Message #3 by Jonathan Gold <jonnygee@p...> on Thu, 18 Apr 2002 09:46:29 -0700
I am having a little trouble with packages. Here is what I have and what is
happening:

I have three classes: mcmLength, tkgWeight, and Carpet. The first and second
are working responses to the exercises at the end of chapter 5. Carpet is
part of a working response to the last exercise of the same chapter; it
makes use of both mcmLength and tkgWeight and it computes the total weight
of a single carpet of such-and-such dimensions and such-and-such unit
weight.

Each of these classes is declared as public.

Now here is what I don't understand: all of these work properly and I am
able to compute total weights of carpets and so on. These three classes are
in a single directory, called Measures, and none contain any import
statements. Now I add a single first line to each class: "package
Measures;". They are in the same package; they each have this package
statement.

Now Carpet no longer compiles: it claims it cannot resolve any of the
references to mcmLength and/or tkgWeight.

I thought that classes in the same package could see each other without
further ado. What am I misunderstanding? What do I have to do to enable the
class Carpet to use the classes tkgWeight and mcmLength?

Before I put them into a package they worked fine together.

Thanks for any help,

Jonathan

Jonathan Gold
jonnygee@p...


  Return to Index