-2

Consider the following directory structure:

enter image description here

The two files main.java and maintest.java are on different locations on disk and yet they declare the same package.

Since they declare the same package name, does this mean they are the in the (virtually)same package i.e. java compiler views them as one package and treats them as such, or are they differentiated in some way, if so how?

Also, considering that we declared a static method called printdate in main.test, how would we import the main.java's public class and use the printdate method in maintest.java?

juztcode
  • 1,196
  • 2
  • 21
  • 46
  • Possible duplicate of [Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld](https://stackoverflow.com/questions/14132602/exception-in-thread-main-java-lang-noclassdeffounderror-helloworld) – Elias Sep 09 '19 at 13:11
  • With that structure, the classes are in package `xyz`, not in package `com.ge.xyz`. – Mark Rotteveel Sep 09 '19 at 14:11
  • oops, sorry my bad, I'll nest the folder structure – juztcode Sep 09 '19 at 16:36

1 Answers1

2

Provided that directory1 and directory2 are on the classpath, then yes Main.java and Maintest.java are in the same package. The filesystem layout doesn't matter in these cases (except the package structure must match the directory structure, in your picture it doesn't). You could also have a jar file on the classpath containing more files belonging to the same package.

You import and use them as you do normally with any classes.

second
  • 4,069
  • 2
  • 9
  • 24
Kayaman
  • 72,141
  • 5
  • 83
  • 121