DEV Community

loading...

Discussion on: Can I import local class files in Java, like you can Python?

Collapse
thefern profile image
Fernando B 🚀

If the class is in the same package you shouldn't have to do anything. If is in another package then you need to specify the package not the folder.

import anotherpackage.someClass;
Enter fullscreen mode Exit fullscreen mode
Collapse
baenencalin profile image
Calin Baenen Author

Will it be able to distinguish if the package is one directory down?
Like this:

Other code:
    // Files...
package 1: // I want to import package 2 here.
    somefile.java
    package 2:
        classes.class

Enter fullscreen mode Exit fullscreen mode
Collapse
thefern profile image
Fernando B 🚀

There isn't such thing as nested packages like you've shown. Packages are all independent of each other. Here's an explanation of hierarchy stackoverflow.com/questions/265092...

Thread Thread
baenencalin profile image
Calin Baenen Author

But, can a package be in a directory below the current package?
If not, where would the packages have to be, and how would I enforce this on to the computer of the user that uses my program?

Thread Thread
thefern profile image
Fernando B 🚀

Sorry is been a while since I last program in Java memory is a bit blurry. According to SO you can't have nested packages, but I just tried and I was able to create a package inside a package. So what you're trying to do is doable.

studytonight.com/java/subpackage-a...