In C# you can mark a class as internal
so that it is only accessible from within the same package. Is there anything similar in Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Yes, the default (package private) access level. Just leave out any access modifier on your class definition and you get what you want.
Dropping the access modifier is similar to internal in C#.
C#
Java
Source: http://www.javacamp.org/javavscsharp/internal.html
this Question has an accepted answer before but I think the answer is not fully clear for some one who is new in java coming from .Net.
Does java have some exact equivalent for internal modifier in C# ? short answer is NO (but you can achieve it some how and i will tell)!!
internal in C# is actually an "assembly-private" modifier. what is an assembly ?
Assembly is any product of your project (DLL or EXE in C# - equivalent in java can be a JAR file)
there is not any exact equivalent for internal in java. and what has been answered by Bryan Kyle and accepted is actually "package-private" (packages in java are equivalent for namespaces in C#) but some how what has been answered is the closest way to get the same result.
BUT how to get a fully same result as internal ? cant java have a solution for this ? the answer is YES.
it does have. but not with a modifier. actually the best way to do this is a secret in packaging. the best practice of packing is to pack your classes by their relation not by their type.
many of us use packages named "Models" or "Presenters" and put all our models or presenters in them. while this is so damn wrong. packages should be like "Book" containing "BookModel.java", "BookPresenter.java" , .....
this way you can make them package-private by omitting the modifier and have not any problem because you can access them in any class which need your package-private class because you have a good practice of packaging.
You can create package-private classes by omitting the security modifier (public, private) from the class's declaration.
I'm not familiar with C#, but in Java the default protection is that something is only accessible within the package:
public=accessible by anyone
private=accessible only within the current class
protected=accessible within the package or in any class that inherits from the current class
default=accessible within the package
I've always thought there should be a way to say "accessible by any class that inherits from the current class but not from anywhere else, this package or any other". But there isn't.
Yes. It's called package private, you just define the class without any modifiers:
package com.blah; class Foo{ }