Solved

understanding permission access using packages

Posted on 2004-09-08
4
181 Views
Last Modified: 2010-03-31
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


0
Comment
Question by:mitchguy
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
gloverkcn earned 50 total points
ID: 12010917
That is by design.

import mypackage.*;
only imports java classes under the mypackage package not the mypackage.subpackage.

The idea is that heavily dependant classes are contained within a single package, and that additional sub-packages generally expand the higher level packages and/or provide specialized functionality.

Finding that there are alot of interdependencies between packages is usually a good indication that the design needs to be reworked.

Hope this helps.




0
 

Author Comment

by:mitchguy
ID: 12011099
>>Finding that there are alot of interdependencies between packages is usually a good >>indication that the design needs to be reworked.
 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



0
 
LVL 1

Expert Comment

by:gloverkcn
ID: 12017247
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;
   }
}

///////////////SubPackageClass
package mypackage.subpackage;
public class SubPackageClass
{
   public int i = 4;
 }

maybe it would be easier if you posted your code.
0
 

Author Comment

by:mitchguy
ID: 12020725
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
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
split string containing \r\n in Java 46 44
Error trying to install RTMT Win7 5 60
replace a word with other 1 44
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question