aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes package naming and imports question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "package naming and imports question" Watch "package naming and imports question" New topic
Author

package naming and imports question

Niels Bautista
Greenhorn

Joined: Nov 05, 2007
Posts: 8
Hi all,
Im reading K&B as a preparation. It is telling me that the following piece of code is not a valid import-statement:


Im wondering why not? I could define a class that resides in the directory structure java/util/ArrayList/Foo.java and the above piece of code would compile just fine.

What Im I missing here?
adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
first what do you wanna do?

if you want to use the ArrayList class, you get the nearly right statement, except it should be like this..

import java.util.ArrayList;

if you want to put your foo.class into the java.util.ArrayList, i dont think that is practically not possible - the compile will complain...
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Originally posted by Johnny Klarik:
I could define a class that resides in the directory structure java/util/ArrayList/Foo.java

ArrayList and Foo are both classes, you cannot package a class inside a class.

Valid import statements are:
adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
assuming I have all java and class files in a folder named SCJP on the windows desktop,and
I package the class file with "package java.util;
so to invoke the file,
the command would be..
javac -cp /Documents and Settings/Administrator/Desktop/scjp java/util/Foo.java
Niels Bautista
Greenhorn

Joined: Nov 05, 2007
Posts: 8
Originally posted by adam lui:
first what do you wanna do?

if you want to use the ArrayList class, you get the nearly right statement, except it should be like this..

import java.util.ArrayList;

if you want to put your foo.class into the java.util.ArrayList, i dont think that is practically not possible - the compile will complain...


I am trying to understand why it would be illegal to use the import statement


With the above statement, I am NOT trying to import the class ArrayList from the java.util package. Instead, Im trying to import alle classes in the java.util.ArrayList package (which I could make myself).

I can construct an example where this is a perfectly legal import; a java file named Foo in package java.util.ArrayList. Just make the directory structure java/util/ArrayList/ with file Foo.java. It *will* work.

[ November 05, 2007: Message edited by: Johnny Klarik ]
[ November 05, 2007: Message edited by: Johnny Klarik ]
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Yes, of course it'll work if you actually create your own java.util.ArrayList package. (note that Java will automatically deconflict package names and class names.) But the point that K&B makes is that you can't apply the .* suffix to a classname in a non-static import statment. You're not supposed to add your own classes or packages to the java.* packages, so that's why they chose that particular class as an example.

But, yes, if you did create a package with the same name as a class, then what you did is perfectly valid syntax. (note however that this means you're violating the naming convention of keeping package names all lowercase.)


SCJP 5.0
Xavier Lio
Greenhorn

Joined: Oct 07, 2007
Posts: 9
where is your package declaration ?

check it may be will makes it OK
Niels Bautista
Greenhorn

Joined: Nov 05, 2007
Posts: 8
Originally posted by Kelvin Lim:
Yes, of course it'll work if you actually create your own java.util.ArrayList package. (note that Java will automatically deconflict package names and class names.) But the point that K&B makes is that you can't apply the .* suffix to a classname in a non-static import statment. You're not supposed to add your own classes or packages to the java.* packages, so that's why they chose that particular class as an example.

But, yes, if you did create a package with the same name as a class, then what you did is perfectly valid syntax. (note however that this means you're violating the naming convention of keeping package names all lowercase.)


Kelvin,
I see the point you and K&B are making. Thanks.

Another point im wondering about, I haven't read anything about how a package identifier is defined lexicograpically in K&B (should not begin with a digit, etc.). Shouldn't I know this for the exam?
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Hi Johnny,

Actually, it turns out that I was mistaken in my belief that Java will automatically de-conflict type names and package names. According to the Java Language Specification:

The members of a package are subpackages and all the top level (�7.6) class (�8) and top level interface (�9) types declared in all the compilation units (�7.3) of the package.

...

A package may not contain two members of the same name, or a compile-time error results.

Here are some examples:

* Because the package java.awt has a subpackage image, it cannot (and does not) contain a declaration of a class or interface type named image.

I verified this behavior with my compiler, which does issue an error if I attempt to declare a package named java.util.ArrayList. My apologies for the misinformation; I must have gotten this mixed up with Java's de-conflicting of type names and variable names (which allows for strange statements such as "foo foo = new foo(foo());").

Anyway, in response to your question about valid package names: if you split the package name into sub-tokens using the periods (.) as delimiters, the rules for each sub-token are exactly the same as for other Java identifiers.
[ November 06, 2007: Message edited by: Kelvin Lim ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package naming and imports question