如何为Java 6枚举实现values()? [英] How is values() implemented for Java 6 enums?

查看:94
本文介绍了如何为Java 6枚举实现values()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public enum Letter {
A,B, C,D,E,F,G;

static {
for(Letter letter:values()){
// do something with letter
}
}
}

此问题涉及values()方法。具体来说,它如何实现?通常,我可以使用Eclipse中的F3或CTRL +点击跳转到Java类的源代码(即使是类,如String,Character,Integer,甚至Enum)。可以查看其他枚举方法的源(例如,valueOf(String))。



values()是否每次创建一个新数组调用?如果我将它分配给一个局部变量,然后修改其中的一个元素,会发生什么(显然这不会影响values()返回的值,这意味着每次都会分配一个新的数组)。



它的代码是原生的吗?或者JVM /编译器特别处理它,只有当它不能证明它不会被修改时,才从values()返回一个新的实例。

解决方案

基本上,编译器(javac)在编译时将您的枚举转换为包含所有值的静态数组。当你调用values()时,它给你这个数组的.clone'd()副本。



给定这个简单的枚举:

  public enum Stuff {
COW,POTATO,MOUSE;
}

您可以查看Java生成的代码:

  public enum Stuff extends Enum< Stuff> {
/ * public static final * / COW / * = new Stuff(COW,0)* /,
/ * public static final * / POTATO / * = new Stuff(POTATO 1)* /,
/ * public static final * / MOUSE / * = new Stuff(MOUSE,2)* /;
/ * synthetic * / private static final Stuff [] $ VALUES = new Stuff [] {Stuff.COW,Stuff.POTATO,Stuff.MOUSE};

public static Stuff [] values(){
return(Stuff [])$ VALUES.clone();
}

public static Stuff valueOf(String name){
return(Stuff)Enum.valueOf(Stuff.class,name);
}

private Stuff(/ * synthetic * / String $ enum $ name,/ * synthetic * / int $ enum $ ordinal){
super($ enum $ name, $枚举$序);
}
}

你可以看看javac如何翻译你的类通过创建临时目录并运行:

  javac -d< output directory> -XD-printflat filename.java 


In Java, you can create an enum as follows:

public enum Letter {
    A, B, C, D, E, F, G;

    static {
       for(Letter letter : values()) {
          // do something with letter
       }
    }
}

This question concerns the "values()" method. Specifically, how is it implemented? Usually, I could jump to the source for Java classes using F3 or CTRL+Click in Eclipse (even for classes like String, Character, Integer, and even Enum). It is possible to view the source of the other enum methods (e.g., valueOf(String)).

Does "values()" create a new array each time it is invoked? If I assign it to a local variable and then modify one of the elements, what happens (clearly this won't affect the value returned by values(), which implies that a new array is allocated each time).

Is the code for it native? Or does the JVM / compiler treat it specially, only returning a new instance from values() when it cannot prove that it will not be modified.

解决方案

Basically, the compiler (javac) translates your enum into a static array containing all of your values at compile time. When you call values(), it gives you a .clone'd() copy of this array.

Given this simple enum:

public enum Stuff {
   COW, POTATO, MOUSE;
}

You can actually look at the code that Java generates:

public enum Stuff extends Enum<Stuff> {
    /*public static final*/ COW /* = new Stuff("COW", 0) */,
    /*public static final*/ POTATO /* = new Stuff("POTATO", 1) */,
    /*public static final*/ MOUSE /* = new Stuff("MOUSE", 2) */;
    /*synthetic*/ private static final Stuff[] $VALUES = new Stuff[]{Stuff.COW, Stuff.POTATO, Stuff.MOUSE};

    public static Stuff[] values() {
        return (Stuff[])$VALUES.clone();
    }

    public static Stuff valueOf(String name) {
        return (Stuff)Enum.valueOf(Stuff.class, name);
    }

    private Stuff(/*synthetic*/ String $enum$name, /*synthetic*/ int $enum$ordinal) {
        super($enum$name, $enum$ordinal);
    }
}

You can look at how javac 'translates' your classes by making a temporary directory and running:

javac -d <output directory> -XD-printflat filename.java

这篇关于如何为Java 6枚举实现values()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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