mitchguy
asked on
understanding permission access using packages
I recently organized my application into seperate packages and directories.
the top level file containing main imported all of these packages i.e.
import mypackage.*;
where mypackage contained numerous related classes
I had a long permission changing and explicit import debugging process before i was
able to get everything to compile again under this new organization
what I didn't understand was why many of the classes in the package,
which I was attempting to access in a nested class in the main file needed to be
imported explicitly before the compiler would find it.
import mypackage.class1;
why didn't the import mypackage.*; find all of them?
is that by design or is it probably something I did wrong?
I had to explicitly import about 20 classes of the package
the top level file containing main imported all of these packages i.e.
import mypackage.*;
where mypackage contained numerous related classes
I had a long permission changing and explicit import debugging process before i was
able to get everything to compile again under this new organization
what I didn't understand was why many of the classes in the package,
which I was attempting to access in a nested class in the main file needed to be
imported explicitly before the compiler would find it.
import mypackage.class1;
why didn't the import mypackage.*; find all of them?
is that by design or is it probably something I did wrong?
I had to explicitly import about 20 classes of the package
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I misunderstood your originally question. Looking at your example I have a couple of Questions.
Are you trying to access createServerThread() as a method?
The
ma.createServerThread()
should look more like
createServerThread thread = new createServerThread(); ?
If that was just a typo then everthing else should work. Heres a working sample I created.
//////////////////// Main class
package mypackage;
import mypackage.subpackage.*;
public class MainClass
{
public static void main(String[] args)
{
StaticNestedClass snc = new StaticNestedClass();
System.out.println("snc i " + snc.i);
}
public static class StaticNestedClass extends Thread
{
SubPackageClass object = new SubPackageClass();
int i = object.i;
}
}
///////////////SubPackageC lass
package mypackage.subpackage;
public class SubPackageClass
{
public int i = 4;
}
maybe it would be easier if you posted your code.
Are you trying to access createServerThread() as a method?
The
ma.createServerThread()
should look more like
createServerThread thread = new createServerThread(); ?
If that was just a typo then everthing else should work. Heres a working sample I created.
//////////////////// Main class
package mypackage;
import mypackage.subpackage.*;
public class MainClass
{
public static void main(String[] args)
{
StaticNestedClass snc = new StaticNestedClass();
System.out.println("snc i " + snc.i);
}
public static class StaticNestedClass extends Thread
{
SubPackageClass object = new SubPackageClass();
int i = object.i;
}
}
///////////////SubPackageC
package mypackage.subpackage;
public class SubPackageClass
{
public int i = 4;
}
maybe it would be easier if you posted your code.
ASKER
I think your first comment applies to what my problem was.
>>import mypackage.*;
>>only imports java classes under the mypackage package not the >>mypackage.subpackage.
I was putting in the classpath the package directories /myApp/mypackage
I changed it to /myApp and now everything works as I expected
>>import mypackage.*;
>>only imports java classes under the mypackage package not the >>mypackage.subpackage.
I was putting in the classpath the package directories /myApp/mypackage
I changed it to /myApp and now everything works as I expected
ASKER
my problem didn't seem to be dependencies between different packages, it was more just using the classes in the package. for example
I'll generalize for brevity
import mypackage.*;
main()
{
myApp ma = new myApp();
ma.createServerThread();
}
class myApp
{
public static class createServerThread() extends Thread
{
packageClass pc = new packageClass();
//instancing the class worked fine as long as I defined it as public
//it was when I tried to do the following that it gave me an bad class error
int getValue = pc.field1;
//it appeared as though it gave me an error trying to access a field
//I made the field public , but it wasn't until I did
//import mypackage.packageClass; that the error went away
}
}
i'm not exactly sure what you mean by?
>>import mypackage.*;
>>only imports java classes under the mypackage package not the >>mypackage.subpackage.
I just realized in using my more detailed example what my problem may have had to do with.
Does that fact that the class I was trying to access it from was a
static class make things different in respect to access privileges?
>> public static class createServerThread() extends Thread