【问题标题】:declaringn a variable in java class (private,static,final) [duplicate]在java类中声明一个变量(私有,静态,最终)[重复]
【发布时间】:2016-11-01 07:44:11
【问题描述】:

Java 中有很多关于staticfinal 变量的讨论。 我真的很想知道以下声明之间的区别。好像有点混乱

public class foo() {
    private static final int a;
    private static int b;
    private final int c;
    private int d;
    public static final int e;
    public static int f;
    public final int g;
    public int h;
}

哪个可以在类内部/外部修改/访问?

P.S:In Java, difference between default, public, protected, and private 的问题范围更大。我的重点是一些令人困惑的地方!

【问题讨论】:

  • private 表示它是该类私有的,不能在该类之外访问。

标签: java static private


【解决方案1】:

private 意味着它只能被 foo 类的实例访问。

public 表示可以从拥有对 foo 类实例的引用的任何对象访问它。

static 表示它属于该类,因此它被所有 foo 实例共享。

final 表示不能改变初始值。

final 属性一旦初始化就无法修改。 static 属性可以修改,但请记住,新值由所有实例共享。 private 属性只能由 foo 实例本身修改。

这意味着static final 属性: 不能修改;由所有实例共享。

【讨论】:

    【解决方案2】:
    private static final int a; // accessed only         / inside only
    private static       int b; // accessed and modified / inside only
    private        final int c; // accessed only         / inside only
    private              int d; // accessed and modified / inside only
    public  static final int e; // accessed only         / inside and outside
    public  static       int f; // accessed and modified / inside and outside
    public         final int g; // accessed only         / inside and outside
    public               int h; // accessed and modified / inside and outside
    

    如你所见:

    • static 在这里没有任何作用
    • finalaccessed and modified 减少到 accessed only
    • private/public 确定inside only / inside and outside

    【讨论】:

    • 您可能想要限定您的断言“静态在这里没有任何影响”,以澄清您的意思是就可访问性而言它没有任何影响。当然,它确实会在其他方面产生影响。
    • @KlitosKyriacou:是的,我确实在发布答案后立即考虑了这一点。但是,我认为 here 这个词暗示了它,除此之外(甚至更有说服力)——关键字没有任何效果是没有意义的,所以我认为很明显“没有效果”在问题的背景......你怎么看?
    【解决方案3】:

    public 属性可以从任何类访问。

    private 属性只能在声明它的类中访问。 (这就是为什么我们需要在其他类中包含 getter 和 setter 来检索私有变量)

    final 属性不能被修改并设置为不同的值。

    static 属性在类本身及其实例中被访问。

    【讨论】:

    • 为什么要投反对票?1
    【解决方案4】:

    在类标识符的上下文中,语句的含义如下所述: 首先privatepublic 关键字是访问符号,这意味着所有带有private 关键字的成员仅在它们声明的类的范围内可见。所有带有public 关键字的成员在类范围之外都是可见的。

    例如

    class foo{
          public int myVar;
          private int myBar;
    }
    

    现在如果你实例化 `foo'

    foo f = new foo();
    f.myVar // is valid
    f.myBar // is invalid and generates compile time error.
    

    static关键字表示标识符的分配对于类的所有实例是通用的,所以这个标识符是在编译器第一次遇到类型定义的时候分配的。由于任何类的类型分配只发生一次,因此只维护了一个静态字段实例,可以跨该类的所有对象访问。

    final 关键字表示(在标识符的上下文中),这些标识符可以初始化一次,然后关闭以进行任何修改。 (在方法的上下文中,这意味着该方法不能被派生类覆盖)。

    现在回到您的问题,以下陈述将意味着:

    private static final int a; 
    // 'a' has scope local to the class and its value is accessible through Type
    // and shared across all instances of that type and its value cannot be
    // changed once initialised. 
    private static int b;
    // 'b' has scope local to the class and its value is accessible through Type
    // and shared across all instances of that type.
    private final int c;
    // 'c' has scope local to the class and its value cannot be changed once 
    // initialised.
    private int d;
    // 'd' has scope local to the class
    public static final int e;
    // 'e' has scope beyond the class, it can be accessed outside the class 
    // through an instance of this class and its value is accessible through
    // Type and shared across all instances of that type 
    // and its value cannot be changed once initialised. 
    public static int f;
    // 'f' has scope beyond the class, it can be accessed outside the class and   
    // value is accessible through Type and shared across all instances of that
    // type
    public final int g;
    // 'g' has scope beyond the class, it can be accessed outside the class
    // through an instance of this class 
    // and its value cannot be changed once initialised.
    public int h;
    // 'h' has scope beyond the class, it can be accessed outside the class
    // through an instance of this class
    

    干杯!

    【讨论】:

      【解决方案5】:

      public- 声明为公共的类、成员方法、构造函数、接口等可以从任何其他类访问。​​可以说它具有全局访问权限。

      private-成员方法、成员变量和声明为私有的构造函数只能在声明的类本身内访问。如果类中存在公共getter方法,则声明为私有的变量可以在类外访问。

      final-final 会确保该字段是一个常量,不能更改

      static-它与类型相关联,而不与实例相关联。即所有对象只存在一个字段副本,而不是每个对象的单独副本。意味着字段的单个副本将在该类的所有对象之间共享

      static final-该类的所有实例将共享相同的值,并且在第一次初始化后无法修改。

      【讨论】:

        【解决方案6】:
        1. private 表示课外看不到。
        2. static 表示变量与类关联,而不是对象。这意味着对于任何对象,其静态变量将由同一类的所有对象共享。 (more information)
        3. final 表示一旦给变量赋值,就不能重新赋值。

        您只能从类外部访问public 变量,但不能修改final 变量。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-25
          • 1970-01-01
          • 2017-08-04
          • 2021-10-30
          • 2018-07-12
          • 2012-05-09
          • 2011-11-10
          • 1970-01-01
          相关资源
          最近更新 更多