• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between import p1.* and import p1.T1  RSS feed

 
Mike Lin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,all,I meet a confused compile question today.
create two java source file named T1.java and T2.java in the same directory,then comiple them.
T1.java

T2.java

compile T1 first ,then compile T2.Nothing exceptions happen.
But change to in T2.java,compile error happens.

compile messages like these:

When I remove the file T2.java,compile succeeds.
Yet ,what's the diffrence difference between import p1.* and import p1.T1?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Things go wrong because the package structure and the directory structure are wrong.

You first say: "create two java source file named T1.java and T2.java in the same directory". Then you show us two source files: T1.java which contains a class T1 in package p1, and T2.java which contains a class T2 in the default package.

That's wrong; if T1 is in package p1, it should have been in a subdirectory named p1, and not in the same directory as T2.java. The directory structure must match the package structure of your classes.

The compiler is telling you that in the error message:

bad class file: .\T1.java
file does not contain class T1
Please remove or make sure it appears in the correct subdirectory of the classpath.
[ October 10, 2006: Message edited by: Jesper Young ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Mike Lin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you ,Jesper .
Yet ,when I comiple T1.java ,I user param "-d ." .so package p1 is auto created after compile.
The focus here is not how to compile it successfully but WHY WE CANNOT REPLACE import p1.* WITH import p1.T1 ??

what's the diffrence between them?

bty,Paul ,I think it's a advaned question.
I have passed SCJP1.4 four yeas ago!

[ October 10, 2006: Message edited by: Mike Lin ]

[ October 10, 2006: Message edited by: Mike Lin ]
[ October 10, 2006: Message edited by: Mike Lin ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHY WE CANNOT REPLACE import p1.* WITH import p1.T1 ??

I don't know, but the question isn't really relevant anymore. If you do something wrong, the compiler gets confused and all kinds of unexpected things may happen, as you have experienced...
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's wrong; if T1 is in package p1, it should have been in a subdirectory named p1, and not in the same directory as T2.java. The directory structure must match the package structure of your classes.

No, I don't think it is wrong. With this directory structure:

C:
----javaPrograms
----------p1
--------------T1.java(package p1
--------------T2.java(no package statement, import p1.T1

since T2 does not have a package statement it will be placed in the default package inside the p1 directory. I was able to compile and run T2.java, so apparently T2 can happily live in a default package inside the p1 directory. The file locations may not be recommended, but apparently there is no technical reason why you can't organize your files that way.

WHY WE CANNOT REPLACE import p1.* WITH import p1.T1 ??

I couldn't duplicate your problem as both worked for me.


You don't have to compile T1 first. When you compile T2, javac will compile T2 because T2 refers to T1.

T2.java:


I suggest you write a T2 class that you can actually execute, i.e one with a main() function. In conjunction with that, I suggest you add a method to T1, so that you can call it when you create a T1 object.

I used these classes with the directory structure above:

T1.java:

T2.java:


and these compile commands:

cd C:\javaPrograms\p1
javac -classpath C:\javaPrograms T2.java
java -classpath C:\javaPrograms;. T2
[ October 11, 2006: Message edited by: sven studde ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I don't think it is wrong. With this directory structure:

C:
----javaPrograms
----------p1
--------------T1.java(package p1
--------------T2.java(no package statement, import p1.T1


But if you carefully look at Mike's first post, you'll see that his directory structure doesn't look like that. He has T1.java and T2.java in the directory D:\temp - so T1.java is not in a subdirectory named p1, as it should be.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Lin:
...WHY WE CANNOT REPLACE import p1.* WITH import p1.T1 ??...

(It's the other way around.)

I tried to answer this yesterday under your cross-post in the SCJP forum. Please don't cross-post. To keep things in one place, here's how I responded...


This is a little sticky, but I'll try it.

"A single-type-import declaration imports a single type by giving its canonical name, making it available under a simple name..." (JLS - 7.5.1) So with import p1.T1; the canonical name is easily resolved, and makes T1 available under its simple name.

But a "type-import-on-demand [i.e. "wildcard"] declaration allows all accessible (�6.6) types declared in the type or package named by a canonical name to be imported as needed." (JLS - 7.5.2) So with import p1.*; the compiler needs to go looking for what you mean by the simple name T1. It starts in the current directory (denoted by "."). And indeed, there is a T1.class in the current directory (p1).

But now the compiler says...
bad class file: ./T1.class
class file contains wrong class: p1.T1


The compiler is finding T1 in the current directory (./T1). But this considered a "bad class file" because T1 is really in a package called p1, and what the compiler thinks it's found (./T1) isn't matching what that file actually contains (p1.T1).

So the compiler makes a suggestion...
Please remove or make sure it appears in the correct subdirectory of the classpath.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
...But if you carefully look at Mike's first post, you'll see that his directory structure doesn't look like that. He has T1.java and T2.java in the directory D:\temp - so T1.java is not in a subdirectory named p1, as it should be.

Good catch! I assumed that the current directory was p1.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if you carefully look at Mike's first post, you'll see that his directory structure doesn't look like that. He has T1.java and T2.java in the directory D:\temp - so T1.java is not in a subdirectory named p1, as it should be.

Ok, I see what you are saying.
[ October 11, 2006: Message edited by: sven studde ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!