Is there anything like an Internal class in Java?

2019-01-17 19:24发布

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?

7条回答
劳资没心,怎么记你
2楼-- · 2019-01-17 19:43

Yes, the default (package private) access level. Just leave out any access modifier on your class definition and you get what you want.

查看更多
Evening l夕情丶
3楼-- · 2019-01-17 19:44

Dropping the access modifier is similar to internal in C#.

C#

public class A
{
    public static int X;
    internal static int Y;
    private static int Z;
}
internal class B
{
    public static int X;
    internal static int Y;
    private static int Z;
    public class C
    {
        public static int X;
        internal static int Y;
        private static int Z;
    }
    private class D
    {
        public static int X;
        internal static int Y;
        private static int Z;
    }
}

Java

public class A
{
    public static int X;
    static int Y;
    private static int Z;
}
class B
{
    public static int X;
    static int Y;
    private static int Z;
    public class C
    {
        public static int X;
        static int Y;
        private static int Z;
    }
    private class D
    {
        public static int X;
        static int Y;
        private static int Z;
    }
}

Source: http://www.javacamp.org/javavscsharp/internal.html

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-17 19:45

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.

查看更多
女痞
5楼-- · 2019-01-17 19:54

You can create package-private classes by omitting the security modifier (public, private) from the class's declaration.

package com.sample;

class MyPackagePrivateClass
{
    ...
}
查看更多
干净又极端
6楼-- · 2019-01-17 19:55

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.

查看更多
不美不萌又怎样
7楼-- · 2019-01-17 20:03

Yes. It's called package private, you just define the class without any modifiers:

package com.blah; class Foo{ }

查看更多
登录 后发表回答