Solved

understanding permission access using packages

Posted on 2004-09-08
4
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

627 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