简体   繁体   English

声明私有静态最终变量的原因

[英]Reasons for declaring a private static final variable

Why would you declare a static final variable as private for an immutable type? 为什么要将static final变量声明为不可变类型的private变量?

Could it possibly do any harm to declare them public on any case? 在任何情况下宣布public可能有害吗?

因此,没有人可以从外部访问它并依赖于该值,让您可以自由地更改它而没有副作用的风险(至少在声明它的类之外)。

There are serveral reasons... 有几个原因......

Privacy 隐私

Keep implementation details hidden from the clients, for example constants for internal use only and with no use for clients 保持对客户端隐藏实现细节,例如仅供内部使用的常量,而不用于客户端

Security 安全

Protect your code from maliscious client codes for example: 保护您的代码免受恶意客户端代码的影响,例如:

static class A
{
    public final static List<String> list = buildList();

    public static List<String> buildList()
    {
        ArrayList<String> list = new ArrayList<>();
        list.addAll(Arrays.asList("A", "B", "C"));
        return list;
    }
}

static class B
{
    public static void main(String[] args)
    {
        A.list.clear();
        System.out.println(A.list);
    }
}

You dont want any one to manipulate you internal data. 你不希望任何人操纵你的内部数据。

It's just best OOP practice to keep variables within the proper scope. 将变量保持在适当的范围内是最好的OOP实践。 An example would be a global static final integer for performing a number of calculations within the class. 一个例子是全局静态最终整数,用于在类中执行多个计算。 Having it exposed (public) not only could cause problems, but confusion as well. 暴露(公开)不仅可能导致问题,而且可能造成混乱。

Say you had a class where you needed to know the value of pi: 假设您有一个课程,您需要知道pi的值:

public class MyClass {
    private static final double PI = 3.14159;

    //functions performing calculations with value of PI
}

The value of pi is only needed within MyClass , so leaving it to be exposed makes no sense - all calculations with the variable are done within MyClass . pi的值只在MyClass需要,所以让它暴露是没有意义的 - 变量的所有计算都是在MyClass中完成的。 If you need it again to perform a calculation, it should be done within the class holding the value to keep your code organized. 如果再次需要它来执行计算,则应该在保持值的类中进行,以保持代码的有序性。

public class GUICommon {
    private static final ExecutorService executorServices = Executors.newWorkStealingPool();

    public static void useExecutors(Runnable run) 
    {
    executorServices.execute(run);
    }
}

I used it on this way. 我就是这样用的。

The static modifier, in combination with the final modifier, is also used to define constants. static修饰符与final修饰符结合使用,也用于定义常量。 The final modifier indicates that the value of this field cannot change. 最终修饰符表示此字段的值不能更改。

https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

If you declare the static final variable with public, the variable will be able to see out site its class. 如果使用public声明静态final变量,该变量将能够看到站点的类。 It may cause a potential bug at compile-time if someone try to change its value in other classes. 如果有人试图在其他类中更改其值,则可能在编译时导致潜在的错误。

If you declare the static final variable with private, it is hidden in other classes. 如果使用private声明静态final变量,则它将隐藏在其他类中。 So you can avoid the such a bug. 所以你可以避免这样的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM