import statement
I
//File P1.java
package MyPackage;
public class P1{
public void afancymethod(){
System.out.println("What a fancy method");
}
}
//File P2.java
import MyPackage.*;
public class P2 extends P1{
public static void main(String argv[]){
P2 p2 = new P2();
p2.afancymethod();
}
}
[370 byte] By [
hsiaoka] at [2007-11-27 9:35:25]

Yep, it's code alright. Thanks for sharing!
Sorry, I didn't complete my last post.
The question is:
can you compile P1.java& P2.java and run P2 successfully ?
I can't.
Why P2.java won't compile as is.
Why is it necessary to have import MyPackage.P;
in P2.java in order for P2. java to compile successfully ?
> import statement Yep, I saw it. What's my prize?
> Sorry, I didn't complete my last post.
>
> The question is:
> can you compile P1.java& P2.java and run P2
> successfully ?
> I can't.
>
> Why P2.java won't compile as is.
> Why is it necessary to have import MyPackage.P;
> in P2.java in order for P2. java to compile
> successfully ?
I worked for me. Make sure that P1.java is inside the MyPackage directory, and P2.java is in the same directory as the MyPackage directory.
Correction:I have to haveimport MyPackage P1; in P2.java in order to compile P2.java successfully.Can you make it run as is ?
Prize ? Can you prove that you have the right answer ?
> Prize ? Can you prove that you have the right answer ?Oh come on, send him a pizza.
> Prize ? Can you prove that you have the right answer ?Yep:"import MyPackage.*;"Pizza?
> Correction:
>
> I have to have
> import MyPackage P1;
> in P2.java in order to compile P2.java
> successfully.
>
> Can you make it run as is ?
I'm confused. I compiled and ran this code:
//File P1.java
package MyPackage;
public class P1{
public void afancymethod(){
System.out.println("What a fancy method");
}
}
//File P2.java
import MyPackage.*;
public class P2 extends P1{
public static void main(String argv[]){
P2 p2 = new P2();
p2.afancymethod();
}
}
in the directory structure I gave in my other post, and it ran. I didn't change the code. Are you having trouble getting this to work?
> Pizza?Crosstopic humour: http://forum.java.sun.com/thread.jspa?threadID=5191310
I DO have P1.java & P2.java under directoryMyPackage and I can't compile P2.java as is.Is it possible to do different version of Java ?I'm running Java 1.6.0.May I ask what version of Java are you running ?
> I DO have P1.java & P2.java under directory
> MyPackage and I can't compile P2.java as is.
>
> Is it possible to do different version of Java ?
> I'm running Java 1.6.0.
> May I ask what version of Java are you running ?
I think that's the problem. Since P2 isn't in the MyPackage package, it shouldn't be in the MyPackage directory. It should be up one. ie:
C:\test\MyPackage\P1.java
C:\test\P2.java
OK, I got it run now byputting P2.java directly under the CLASSPATHand P1.java under MyPackage which is directly underCLASSPATH.Thank you for the post, case closed.
I still have question on this:
If I have . the current directory in the CLASSPATH.
and have P2.java & P1.java both under MyPackage directory
and Mypackage is under (not in ) the CLASSPATH.
then
//File P1.java
package MyPackage;
public class P1{
public void afancymethod(){
System.out.println("What a fancy method");
}
}
//File P2.java
import MyPackage.*;
public class P2 extends P1{
public static void main(String argv[]){
P2 p2 = new P2();
p2.afancymethod();
}
}
should both compile and
I should be able to run P2 from under MyPackage.
but it doesn't.
What's wrong here ?
I thought you said the case was closed?Anyway, if both classes are in the same package then you don't need an import statement but you do need the package statement in both classes.