是否有任何目的将私有类变量公开 [英] Is there any purpose to make private class variable to public

查看:132
本文介绍了是否有任何目的将私有类变量公开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我很想知道在Java中将私有类变量设置为public是否有任何目的。

Hello I am curious to know that Is there any purpose to make private class variable to public in Java.

public class XYZ {

    public String ID;
    public ABC abc;

    private class ABC {
        public boolean isExist;
    }
}

提前致谢。

推荐答案

是的,有目的。如果你这样做那么那些可以访问类的程序元素可以直接操作该变量。否则(如果变量是私有的),这些元素仍然可以访问类但不能操作变量(除非你为它提供getter / setter)。

Yes, there's a purpose. If you do that then those program elements which can access the class can manipulate that variable directly. Otherwise (say if the variable is private), those elements would still be able to access the class but won't be able to manipulate the variable (unless you provide a getter/setter for it).

以这种方式思考:类修饰符定义了对类的访问级别,然后变量修饰符定义了对变量本身的访问级别(对于这些元素)可以访问该类)。

Think about it this way: the class modifier defines the level of access to the class, the variable modifier then defines the level of access to the variable itself (for those elements which can access the class).

这篇关于是否有任何目的将私有类变量公开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆