6

I have an Android Studio module called app. I have another library module called library.

Inside library, i have some packages, like manager or network. The package manager contains a class called SPManager with a static method storeSP()

package com.example.library.manager;

public class SPManager {
    ______ static void storeSP(){...}
}

Is it possible make storeSP() accessible only within my library module? If i choose package-private (no access modifier), I cannot access it from my network package in the same module. If I choose public, then this method is also accessible from other modules.

Thanks in advance!

Lukas Lechner
  • 7,881
  • 7
  • 40
  • 53
  • According to the official [tutorial](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html) it doesn't seem like this is possible, but perhaps there's a workaround I'm not aware. This is usually why Java libraries put some classes inside a package with the name `internal`, so consumers of the library are at least aware the API in those classes is internal and might change. Curiously enough, kotlin adds the modifier `internal`, which does exactly what you want. – Fred Mar 22 '19 at 05:14
  • As I suspected, there seems to be some ways of doing this. According to Andre Valenti in this question https://stackoverflow.com/questions/6642909/providing-java-library-but-hiding-some-classes you can use jigsaw modules if you're in Java 9 or use different Maven modules to expose only the public API. – Fred Mar 22 '19 at 05:20

3 Answers3

0

Change the access modifier of your methods to protected so that the scope of the method remain inside the package and the class which extends it.

public class Ex{
    protected void test()
    {
    }
}
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
  • 1
    Thanks, but then I can only access it in the same package. I want to access it from every package inside the module. – Lukas Lechner Sep 17 '15 at 07:35
  • protected methods are methods that can be accessed internally by that class, or classes that inherit from it. protected methods will not help you here. Unfortunately, I'm looking into doing the same thing as the OP. The access modifier in C# is called "internal", not sure what the Java equivalent is. – Glaucus Mar 21 '19 at 14:56
0

Simple answer is that if you mark your method with modifier public, then it will be accessible by any other classes.

If you want to make your particular classes/interfaces be only visible/accessible inside that module, you can consider applying obfuscation on those classes/interfaces. After obfuscation, they can become meaningless package name. By doing this, you can hide your private classes.

Check more from my answers:

shizhen
  • 12,251
  • 9
  • 52
  • 88
0

I was also searching for something similar and thanks to @Fred that I have achieved it. For someone who may be looking for an answer in Kotlin use internal modifier.

There are four visibility modifiers in Kotlin: private, protected, internal and public. The default visibility is public

  • private means visible inside this class only (including all its members).

  • protected is the same as private but is also visible in subclasses.

  • internal means that any client inside this module who sees the declaring class sees its internal members.

  • public means that any client who sees the declaring class sees its public members.

     open class Outer {
    
     private val a = 1
     protected open val b = 2
     internal val c = 3
     val d = 4  // public by default
    
     protected class Nested {
         public val e: Int = 5
      }
    
    }
    
    class Subclass : Outer() {
     // a is not visible
     // b, c and d are visible
     // Nested and e are visible
    
     override val b = 5   // 'b' is protected
     }
    
    class Unrelated(o: Outer) {
     // o.a, o.b are not visible
     // o.c and o.d are visible (same module)
     // Outer.Nested is not visible, and Nested::e is not visible either
     }
    

For detailed explanation and code sample visit here

Nikhil
  • 911
  • 15
  • 28