Solved

understanding permission access using packages

Posted on 2004-09-08
4
175 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now