I have a package MyPack and a compiled public class Balance in it.
I go back to MyJava and create a NewClass that imports MyPack.* and calls a method on a Balance-type object reference. (All members in Balance are public)
NewClass does not compile if I write the import statement as import MyPack.*; returns error:
bad class file c:/MyJava/MyPack/Balance.class class file contains wrong class MyPack.Balance Please remove or make sure it appears in the correct subdirectory of classpath: Balance b=new Balance ("The balance is "+22000);
The error pointer shows at the type Balance in the above line
But if I write the import statement as
everything works fine!!! ???
My Path and classpath respectively are as follows:
I cannot be entirely sure here but what I think you are experienceing is the the javac translator finding another method "Balance" and declaring it to be the one you THINK you are invoking. I think you are susceptable to this type of error because you have used the wildcard (*) in your import statement and this gives the translator free reign to find the first "Balance" method and use that (even if it is in another package). Because you are not fully qualifing your method invokaction.
It's kind of a long shot I know. I suppose you could test it by naming your Balance method to something you know will be unique (e.g. you name or something like that).
Hentay, what if I have 3 or 4 classes in MyPack and want to import all? Then shouldn't I be using * I do have the package statement and I got this example right out of the complete reference so I don't thing the naming should cause a trouble here. But you are right about it though. Thanks
And Jeff right now Balance is the only class in MyPack. I had another class in the package, with an intent to use it later in the pgm, but I deleted everything else in MyPack just to clear this "Balance" thing out first [ November 04, 2004: Message edited by: Sue Mi ]
This might be part of the cause of the problem. You don't need c:\MyJava or c:\MyJava\MyPack in the PATH. The PATH is used to find executable files, such as javac.exe and java.exe when you are not in the same directory as they are.
Also, you should have c:\MyJava in the CLASSPATH, but not c:\MyJava\MyPack. The JVM uses the classpath as the base for finding classes. It then searches for packages as subdirectories from that base. I suspecte that the JVM is getting confused since it can find the Balance class through two different entries in the CLASSPATH, and apparently it is choosing the wrong one (c:\MyJava\MyPack).
Anyway, try removing c:\MyJava\MyPack from the CLASSPATH to see if that fixes your issues with "import MyPack.*".
With that said, I agree whole-heartedly with everyone else. The preferred method is to import only the specific classes you wish to use rather than all of the classes in a package (e.g. with import package.*).
"what if I have 3 or 4 classes in MyPack and want to import all? Then shouldn't I be using *"
I'd still import the 3 or 4 classes individually. Generally speaking there are going to be more classes than that in any package you come across (except perhaps your own when you're starting out) and it would be very rare indeed to be using every single class in a package. The naming convention certainly isn't going to be your problem, I was just pointing it out to make you aware.
Layne, I tried out your suggestions too again. Infact there was no c:/myjava/mypack in classpath initially. I just tried to check if it will work this way. Since you all say the common practice is to import individual classes, I guess I'll just stick with that for now. But the question still bugs me. When everything is okay, shouldn't it work irrespective of whether I import an individual class or a whole package?