Tag: enums

NullPointerException | enum构造函数中的`this`导致NPE

public class Test { public static void main(String[] args) { Platform1 p1=Platform1.FACEBOOK; //giving NullPointerException. Platform2 p2=Platform2.FACEBOOK; //NO NPE why? } } enum Platform1{ FACEBOOK,YOUTUBE,INSTAGRAM; Platform1(){ initialize(this); }; public void initialize(Platform1 platform){ switch (platform) { //platform is not constructed yet,so getting `NPE`. //ie. we doing something like -> switch (null) causing NPE.Fine! case FACEBOOK: System.out.println(“THIS IS […]

为什么Java enum不能是最终的?

public interface Proposal { public static final enum STATUS { NEW , START , CONTINUE , SENTTOCLIENT }; } Java不允许枚举在接口内是final的,但默认情况下,接口内的每个数据成员都是public static final 。 任何人都可以澄清一下吗?

从POJO生成Json Schema

是)我有的: 我正在从pojo生成JSON模式。 我生成模式的代码如下所示: ObjectMapper mapper = new ObjectMapper(); TitleSchemaFactoryWrapper visitor = new TitleSchemaFactoryWrapper(); mapper.acceptJsonFormatVisitor(clazz, visitor); JsonSchema schema = visitor.finalSchema(); schemas.put(clazz, mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema)); 我通过上面的代码生成了几个模式。 其中一个pojos有一个内部嵌入枚举来限制可能的值,如下所示: public class MyClass { @JsonProperty(“name”) private String name; @JsonProperty(“startDayOfWeek”) private MyClass.StartDayOfWeek startDayOfWeek; /** * The ID of a timezone returned by the timezones route. * */ @JsonProperty(“timezone”) private String timezone; @JsonIgnore private […]

在java中,这样的枚举类型编译成什么?

下面是定义枚举类型的代码。 enum Company{ EBAY(30), PAYPAL(10), GOOGLE(15), YAHOO(20), ATT(25); private int value; private Company(int value){ super(this.name()); this.value = value; } public int getValue(){ return value; } } 内部编译到, final class Company extends Enum{ public final static Company EBAY = new Company(30); public final static Company PAYPAL = new Company(10); public final static Company GOOGLE = new […]

消除重复的枚举代码

我有大量实现此接口的Enums: /** * Interface for an enumeration, each element of which can be uniquely identified by it’s code */ public interface CodableEnum { /** * Get the element with a particular code * @param code * @return */ public CodableEnum getByCode(String code); /** * Get the code that identifies an element of the enum * […]

自Java 5以来最好的单例模式

从Java 5开始,据说创建单例的最佳方法是使用单元素枚举类型。 例: public enum SuperSingleton implements Zooma{ INSTANCE; /** */ public void fightTheBattle(){ System.out.println(“I am fighting the battle!!!”); } @Override public void runningWild() { //This is method implemented from the Zooma interface. } } 根据Joshua Bloch的说法,单元素枚举单例是; 更简洁 免费提供序列化机器 并提供反对多实例化的铁定。 我可以看到它是如何更简洁,它如何提供反对多实例化的铁定, 但它如何免费提供序列化机制? 作为一个枚举,这是单身人士得到的东西吗?

如何从枚举构造函数中抛出exception?

如何从枚举构造函数中抛出exception? 例如: public enum RLoader { INSTANCE; private RLoader() throws IOException { …. } } 产生错误 未处理的exception类型IOException

枚举,单身人士和反序列化

枚举被认为是单身人士的最佳方式,其中一个原因是它隐式inheritance了Serializable 。 但是enums如何防止单身人员的反序列化问题?

Enum类型中wrt到构造函数的静态块的执行顺序

这来自Effective Java: // Implementing a fromString method on an enum type private static final Map stringToEnum = new HashMap(); static { // Initialize map from constant name to enum constant for (Operation op : values()) stringToEnum.put(op.toString(), op); } // Returns Operation for string, or null if string is invalid public static Operation fromString(String symbol) { […]

Java:Enums与if-then-else的表现

我没有真正的幸运,通过使用谷歌获得这个比较的简明答案,而不是做我自己耗时的评估,我想我会先问。 我很确定使用Enums的switch语句比if-then-else语句执行得更快,不管它是否是一个明显的区别是另一个问题。 有人可以为我阐明这一点吗? 感谢快速回复的人,我将在未来的项目中牢记这一点。