If i have a class inside a package say mypackage.myclass and want to access a package’s class that is in the same folder as the package’s root (not inside the package) say anothepackage.anotherclass how can I actually do so?
Example presented in code:
// directories are preceded by a DIR, files are preceded by a CLASS
// any level deeper is preceded by a [Tab]
DIR mypackage
CLASS myclass
DIR myinnerpackage
CLASS myinnerclass
DIR anotherpackage
CLASS anotheclass
CLASS yetanotherclass
DIR org
DIR apache
DIR commons
DIR exec
CLASS DefaultExecutor
Inside the file myclass:
package mypackage;
public class myclass{
// simplest example code I can come up with
org.apache.commons.exec.DefaultExecutor exec = new org.apache.commons.exec.DefaultExecutor();
}
Why does this piece of code not work for me?
Generally how can myclass access anotherclass or yetanotherclass and how can myinnerclass access them as well? Try to refrain for imports as I want to use certain members of each class only (some variables have same names and will get things messed)! Thanks in advance!
Java right?
inside myClass
and then inside your myClass you can just use
then using object “ac” you can access any of anotherClass properties.
you might want to read about packages and imports
Edit
without using imports
i you can use concept of fully qualified name try like
then use ac toacees anotherClass properties
by the way you mentioned, different classes have some same variables and imports can mess up stuff. Java works around objects, same names aint really gonna messup. even if some variables are static you will be using classnames.staticVar to access. so not much of messing up. 🙂
UPDATE :
the discussion below was moved to chat as it was getting comprehensive
some usefull snippets from chat
Askee was facing classpath issues
CLASSPATH , as the name suggests it is the path to class.
when we compile a java file, the compiler will look for the resources that the class needs in current directory and on the classpath.
everything cann not be in same directory (actually it can be, 🙂 ) , so we use
to set classpath and where else to look for classes
in the question the ckass myClass was not getting compilec as
suggested to set parent directory on classpath to have it work