Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java's equivalent to C#'s "internal" [duplicate]

In Java, by leaving the access modifier as the default one (blank), the fields becomes accessible to only members in the same package. However, this is not preventing others from declaring their classes in the same package and then accessing the "default" fields from there.

Is there a way in Java to make fields C# equivalent to internal. That is, when I build my library (JAR file), there's no way others can access those fields from outside the JAR? Even when declaring their classes in the same package as my classes.

Here is my declaration in my library:

package com.my.package;
class MyRestrictedClass{
}



What I'm trying to prevent is the user of my library to do from their project with my jar added to their build path something like:

package com.my.package; //Add their class to my package
public class DeveloperClass{
    public void main(){
        MyRestrictedClass foo = new MyRestrictedClass();
    }
}
like image 333
AxiomaticNexus Avatar asked Nov 04 '13 16:11

AxiomaticNexus


People also ask

What is equivalent to pointer in Java?

Java doesn't have pointers; Java has references.

Does Java have unions?

Since the Java programming language does not provide the union construct, you might think there's no danger of implementing a discriminated union. It is, however, possible to write code with many of the same disadvantages.

What does the operator do in Java?

Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. As we explore the operators of the Java programming language, it may be helpful for you to know ahead of time which operators have the highest precedence.


1 Answers

You can seal the package. That prevents people from adding classes to the classpath (though you still have to protect your jars).

http://docs.oracle.com/javase/tutorial/deployment/jar/sealman.html

Having sealed your jar, you can still compile a class which claims to be in the same package, but when you run it, it'll fail to run:

Exception in thread "main" java.lang.SecurityException: sealing violation: can't seal package org.splore.so.access: already loaded
at java.net.URLClassLoader.getAndVerifyPackage(URLClassLoader.java:395)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:417)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.splore.so.access.AccessTestFromOtherPackage.main(AccessTestFromOtherPackage.java:5)
like image 114
CPerkins Avatar answered Sep 20 '22 17:09

CPerkins