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]
# 1
Yep, it's code alright. Thanks for sharing!
hunter9000a at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 2

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 ?

hsiaoka at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 3
> import statement Yep, I saw it. What's my prize?
Navy_Codera at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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.

hunter9000a at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 5
Correction:I have to haveimport MyPackage P1; in P2.java in order to compile P2.java successfully.Can you make it run as is ?
hsiaoka at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 6
Prize ? Can you prove that you have the right answer ?
hsiaoka at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 7
> Prize ? Can you prove that you have the right answer ?Oh come on, send him a pizza.
BigDaddyLoveHandlesa at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 8
> Prize ? Can you prove that you have the right answer ?Yep:"import MyPackage.*;"Pizza?
Navy_Codera at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 9

> 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?

hunter9000a at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 10
> Pizza?Crosstopic humour: http://forum.java.sun.com/thread.jspa?threadID=5191310
BigDaddyLoveHandlesa at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 11
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 ?
hsiaoka at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 12

> 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

hunter9000a at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 13
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.
hsiaoka at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 14

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 ?

hsiaoka at 2007-7-12 23:02:31 > top of Java-index,Java Essentials,Java Programming...
# 15
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.
floundera at 2007-7-21 23:04:03 > top of Java-index,Java Essentials,Java Programming...