Elementary question... multiple source files.

Hi there, this is an easy one.

I just want to understand two concepts: packages (actually, and jar files too) and calling a method in a different .java file.

Annoyingly, I have actually done this before and moved on from it. But I've forgotten not only how I did it, but how I found out how I did it (my Core Java book only contains single source file programs).

So, let's say I have a dead complicated program like this one:

package done;

publicclass Datea{

publicstaticvoid Show(){

System.out.println("Frog.");

}

publicstaticvoid main(String[] args)

{

Show();

}

}

...and I am so overwhelmed by it's complexity that I want to split it into two managable chunks, one with main and the other with Show, so Show is in a seperate file... how do I do it?

I know, stupid, but I'm just guessing right now and none of my guesses work... and to think, I did this fine months ago!

I am assuming that the package identifier at the top doesn't make much difference if it's there or not providing my source files are in the same directory?

..Thanks.

Jon.

Message was edited by:

vapourmile

[1694 byte] By [vapourmilea] at [2007-10-3 4:05:43]
# 1

> I am assuming that the package identifier at the top

> doesn't make much difference if it's there or not

> providing my source files are in the same directory?

Incorrect. It's the package, not the directory that matters. Classes must be in a directory structure that matches their package structure, relative to a classpath root.

// C:\work\pack\C1.java

pakcage pack;

Class C1 {}

// C:\work\pack\C2.java

package pack;

Class C2 {

// I don't need to import or expclicitly specify the package for C1 because they're in the same package.

C1 c1;

public static void main(String[] args) {}

}

In C:\work:

javac pack\*.java

java -cp . pack.C2

OR

java -cp C:\work pack.C2

jverda at 2007-7-14 22:05:04 > top of Java-index,Java Essentials,New To Java...
# 2

Well, thanks for the effort, but I am not really sure what the above answer means as I have succesfully removed the package identifier and placed both my source files in the same direcctory and it works just fine with no packages mentioned. The main() method looks like this:

public class Datea {

public static void main(String[] args)

{

Show.show();

}

}

...and the callout method is in a class like this:

public class Show{

public static void show(){

System.out.println("Frog.");

}

}

Now, all I need to know is, is this good convention? Because the dot syntax in the calling method and the two levels of paranthesis in the called method seem a little bit long-winded, can the called method be written in a shorter file?

vapourmilea at 2007-7-14 22:05:04 > top of Java-index,Java Essentials,New To Java...