• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package naming and imports question

 
Niels Bautista
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Xavier Lio
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where is your package declaration ?

check it may be will makes it OK
 
Niels Bautista
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic