0

I have a project that must be exported as library.

The project has multiple packages and I want that some public methods/classes can't be accessed from the application that will host my library. Quick example:

package test.package1
import test.package2.*;

//Class1 can be accessed from external
public class Class1 {
    public Class1() {}

    public doSomething(){

        //Class2, instead, should be only "library-internal" and not accessed from external
        Class2 c2 = new Class2();
        c2.doSomething();
    }
}

package test.package2

//Library private class
public class Class2 {

     public doSomething(){
     }
}

I've found many post that was speaking about this problem:

But none of these solved my problem in a clear mode (for me) so, what I'm asking, is if what I'm doing wrong is the packages structure or if exists an elegant solution to my problem.

I think that I can quickly resolve the problem just placing all classes in the same package and declaring public only what I want that can be accessed from external, but I don't think that is a good solution and make worse the project readability.

==============

Edit 1: I try to extend my question with a more specific sample

Imagine that I have a package named "test.model" where I put all classes that will interact with a SQLite database.

Now, all classes that are in this package, MUST be accessibile within the library by all others "internal" packages.

Always in the same application I have a package named "test.ui" where I did put all fragments and activities. All these ui components NEED to access to classes present in "test.model" package, but I don't want that "test.model" package can be visible from the application that will host the library.

Activities and fragments present in "test.ui", instead, can be used from the application that is hosting the library.

Below an example of the project structure:

//I want that this package is for library "internal" use only
 --- package test.model 
 ------ Author.java
 ------ Book.java

 //This package can be accessed from project hosting my library
 --- package test.ui
 ----- BookListFragment.java   //Can access, with a private method, to Book class
 ----- AuthorListFragment.java //Can access, with a private method, to Author class
Community
  • 1
  • 1
Danno
  • 93
  • 2
  • 9

1 Answers1

0

You would need to move Class2 into test.package1, then if you change the visibility of Class2 from public to package level (i.e. remove the word 'public' from the class declaration) this should do what you want.

If you really don't want to move classes around packages then you really need to look at more complicated Java OOP interactions.

If you are more specific as to the type of operations your classes are doing you might get better examples.

Lee
  • 738
  • 3
  • 13
  • I've edited my question and tried to be more specific. Thanks anyway for your reply and time. – Danno May 05 '16 at 13:29
  • I think at some point you will need something in your model package to be public - otherwise what is the point of a package with nothing that could be accessed externally. The Java Swing uses model delegate separation and all of its models are public. – Lee May 05 '16 at 14:35
  • Something that might give you what you want is to define the models in terms of interfaces which are effectively always public. The in your model package implement them - however that still leaves the issue of how to create the models... – Lee May 05 '16 at 14:38