File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Understading Java Classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Understading Java Classpath" Watch "Understading Java Classpath" New topic
Author

Understading Java Classpath

Amit K K Amit
Greenhorn

Joined: Oct 10, 2011
Posts: 8

Guys need your help here.
I am refering to Kathy Sierra and Bert Bates SCJP 1.6 certification book. My question is releated to the "Fundamentals" section. I am having difficult time understanding one of the concept given in book under Searching for Other Classes (Exam Objective 7.5).It goes like this :-

"As soon as they find the class they're looking for, they stop searching for that
class. In the case that their search lists contain two or more files with the
same name, the first file found will be the file that is used".

In practical scenario if i have two class under

com.ecorp.account.saving.Utility.java => has method doAccounting() > does calculation for saving specific accounts
com.ecorp.account.checking.Utility.java => has method doAccounting() > does calculation for checking specific accounts

If my test class TestAccounts.java import both the classes and extend one of them(in my case com.ecorp.account.saving.Utility.java) to use doAccounting() .I dont understand where am i violating the contract for compilation. why i am getting the compilation error as {Utility is already defined in a single-type import}. Any support would be of great help.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 2686
You seem to be mixing up two concepts
- finding the definition of a class using classpath
- finding the full name of a class using imports

The statement you quote refers to the first concept. It has nothing to do with the second concept.

Presumably for your actual question you have something like this

The error is saying that you are trying to use the class Utility but you have two imports that refer to a class called Utility. The compiler doesn't know which one you want to use, so it displays an error.

This has nothing to do with the text you quoted. Imagine you have almost the same code but with only one import.

Here the compiler knows which Utility class you are referring to because there is only one import. So now it has to find the definition for that class.
Lets say you have the following file directories
C:\javalibs\v1\com\ecorp\account\saving
and
C:\javalibs\v4\com\ecorp\account\saving
and both those directories contain a Utility.class file.

Now if your classpath was
C:\javalibs\v4;C:\javalibs\v1
then the compiler would get the definition for the Utility class from the second directory because that is the first one in the classpath, but if your classpath was
C:\javalibs\v1;C:\javalibs\v4
then the compiler would get the definition for the Utility class from the first directory because that is the first one in the classpath.

If I've misunderstood your problem then you need to post the code you are using and the exact text of the error you are getting.


Joanne
Pierluigi Di Giacomo
Greenhorn

Joined: Jan 18, 2012
Posts: 13
Amit K K Amit wrote:Guys need your help here.

com.ecorp.account.saving.Utility.java => has method doAccounting() > does calculation for saving specific accounts
com.ecorp.account.checking.Utility.java => has method doAccounting() > does calculation for checking specific accounts

If my test class TestAccounts.java import both the classes and extend one of them(in my case com.ecorp.account.saving.Utility.java) to use doAccounting() .I dont understand where am i violating the contract for compilation. why i am getting the compilation error as {Utility is already defined in a single-type import}. Any support would be of great help.


I think the compiler is complaining because the two import statements are ambiguous in the first place:
they aliased those two different classes with the same identifier "Utility", so the compiler stopped
you. If you were able to compile that code, then adding some statement like "Utility u;" would make
the JVM implode at runtime, because the JVM wouldn't know which actual class type
(...saving.Utility or ...checking.Utility) to declare the variable u.
AFAIK if you want to use them in the same class, then you should use those Utility classes with their full
package name, or import only one of them (and using the word "Utility" only for that) while using the
long name for the other.

Amit K K Amit
Greenhorn

Joined: Oct 10, 2011
Posts: 8


Neal/Pierluigi Di Giacomo
Thanks a lot for your help. You guys answered my question perfectly. I confused myself with the statement and messed up with two different scenario in one.
Once again thanks for helping me out.
Regards,
Amit
 
 
subject: Understading Java Classpath
 
Threads others viewed
Question from Examlab
Passing console input data to an object of a class..
private static inner class
accessing method in another file in another class
Package question
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture