Providing Java library, but hiding some classes

后端 未结 6 1414
鱼传尺愫
鱼传尺愫 2020-12-15 08:32

I am developing an application in Java ME that I want to provide as a library. Is there no way to hide classes that I don\'t want everyone to use, but is essential still in

相关标签:
6条回答
  • 2020-12-15 09:03

    You can make the classes package protected which only other classes in the same package can see. If this isn't feasible, then you can use ProGuard to mangle the classes and hide their implementations.

    0 讨论(0)
  • 2020-12-15 09:07

    You need to make the classes that you don't want exposed protected. This will make them non usable from client code. Read more in the official docs

    0 讨论(0)
  • 2020-12-15 09:12

    Lets consider an Example:

    If you have a class A, that you want to hide, and a class B, that uses the functionality of class A, then you can do this:

    class B{
    //Attribute and Methods
    
        //Inner class A
        class A{
          //Methods and Attributes.
        }
    
    }
    

    After doing this, you can create an Object of class A inside a method of class B and hence use it. Though the class will be hidden from other classes, it could still be used.

    0 讨论(0)
  • 2020-12-15 09:12

    Yes, there is.

    Simply don't declare those classes public. In other words, omit the public keyword like so:

    class Internal { // rather than "public class Internal"
        ...
    }
    

    By default, classes are only accessible within the package where they are defined.

    0 讨论(0)
  • 2020-12-15 09:18

    For setting up your library API you'll want to protect anything you don't want exposed. Do do this just omit the access modifier:

    class fooBar {
        // do stuff here    
    }
    

    This will set the class access as 'default' which allows access from within the same package as well as from any classes which subclass fooBar.

    Within your classes you will also want to lock down any access on your methods and members by marking them either private, protected or omitting the modifier so that they are 'default' as required.

    • private will allow access from the containing class only;
    • 'default' (no modifier) allows from within the containing class and containing package; and
    • protected will allow access from within the same class, package and any subclasses.

    For anything that you have exposed (public) it is also good practice to mark it as final if it's not designed to be overridden.

    Basically, lock down everything as much as you can. Smaller API's are easier to use and harder to break. If you find something needs to be exposed in the future, do it in the future. It's much easier to expand an API rather than deprecate parts of it.

    0 讨论(0)
  • 2020-12-15 09:18

    If Java 9 is possible, use Jigsaw modules. If not, put every class on the same package, with package-level access for hidden classes, and use Maven modules to organize them.

    I've done exactly that in my project called coronata, a Wii Remote java library. Almost all classes are in package com.github.awvalenti.bauhinia.coronata, but on different modules (which appear as projects on the IDE).

    Visible classes are public. They are in modules:

    • coronata-api
    • coronata-builder
    • coronata-demos
    • coronata-lib

    Hidden classes have package-level acesss. They are in modules:

    • coronata-common
    • coronata-implementation-bluecove
    • coronata-implementation-wiiusej
    0 讨论(0)
提交回复
热议问题