代码之家  ›  专栏  ›  技术社区  ›  user4458727

为什么所有的最终变量在默认情况下都不是静态的?

  •  1
  • user4458727  · 技术社区  · 7 年前

    你需要一个 final a中的变量 ? 编译器是否自动分配 static 最终的 变量?

    静止的 最终的 ,我想问的是,您是否需要 final int x 而不是 static final int x x

    5 回复  |  直到 7 年前
        1
  •  2
  •   matoni    7 年前

    关键字 final 只有一次 施工时。

    public class Person {
        public final String name;
    
        public Person(String name) {
            this.name = name;
        }
    }
    
    Person p = new Person("a");
    p.name = "newName"; // COMPILE ERROR name is marked as "final" thus can not be changed.
    

    考虑以下类别:

    public class OriginConstants {
          // static final fields allows to access constants via class accessor i. e. OriginConstants.x (no need to create instance)
          public static final int x = 0;
          public static final int y = 0;
    }
    
    public class ImmutablePoint {
        public final int x;
        public final int y;
    
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    
    public class MutablePoint {
        public int x;
        public int y;
    
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    

    // create immutable point shifted from coordinate system origin by 5
    ImmutablePoint ip = new ImmutablePoint(OriginConstants.x + 5, OriginConstants.y + 5);
    
    // updating point coordinate by 10
    ip.x += 10; // COMPILE ERROR 
    ip.y += 10; // COMPILE ERROR
    
    // we cannot modify final fields, but we can create new instance with shifted coordinates
    ImmutablePoint shiftedIp = new ImmutablePoint(ip.x + 10, ip.y + 10);
    
    // create mutable point shifted from coordinate system origin by 5
    MutablePoint mp = new MutablePoint(OriginConstants.x + 5, OriginConstants.y + 5);
    
    // updating point coordinate by 10
    ip.x += 10; // OK
    ip.y += 10; // OK
    

    对于一些不能及时更改的坐标,我会使用不变点。假设画布的高度为100,宽度为100。我们可以按如下方式创建辅助对象常量点:

    public class Canvas {
        public static final int HEIGHT = 100;
        public static final int WIDTH = 100;
    
        // anchor points
        public static final ImmutablePoint topLeft = new ImmutablePoint(0,0);
        public static final ImmutablePoint topRight = new ImmutablePoint(Canvas.WIDTH, 0);
        public static final ImmutablePoint bottomLeft = new ImmutablePoint(0, Canvas.HEIGHT);
        public static final ImmutablePoint bottomRight = new ImmutablePoint(Canvas.WIDTH, Canavas.HEIGHT);
    }
    

    这样我们就可以确定 topLeft

        2
  •  2
  •   Florian Fankhauser    7 年前

    最终变量的一个重要用法是 immutable objects .不可变对象是指一旦创建并初始化,就永远不会发生变异的对象。

    简单的不可变类示例:

    class User {
      private final String name;
    
      public User(String name) {
        this.name = name;
      }
    
      public String getName() {
        return name;
      }
    }
    
        3
  •  1
  •   GhostCat    7 年前

    因为这很有道理 final .

    实际上,那应该是你的 要做的事。你只需放下 最终的

    推理很简单:当您忘记初始化字段时,您希望编译器告诉您。除此之外:您甚至可以在那里或构造函数内初始化字段。因为这样可以确保在所有其他代码中,所有字段都已正确初始化。

    immutability 类的属性!

        4
  •  0
  •   JP Belanger    7 年前

    这样做是为了防止方法修改对象实例中的状态。它们被称为不可变对象,通常会简化您的编程,因为它们将简化您的测试场景。

    这在这里得到了很好的回答: What is meant by immutable?

    public class Person {
        public final String name;
    
        public Person(String name) {
            this.name = name;
        }
        public Person rename(String name) {
            return new Person(name)
        }
    
     }
    
        5
  •  0
  •   SomeDude    7 年前

    static 意味着您不必拥有类的实例才能使用变量。

    Math.PI ,您不需要实例化 Math PI 因此,它是静态的。它必须是常数,所以它也是最终的。

    final 如果您希望在不实例化类的情况下使用常量。