java enum 常量相关方法记录

对于java enum 静态方法的理解。仅供自己参考。仅作记录。发现自己更习惯将博客当做自己的笔记。所以对于他人的借鉴意义不是很大。不过自己不想为了分享而分享。

线索如下:


package com.zhi.learnj2;

import java.util.Date;

import com.zhi.utils.DateUtil;

public enum ConstantSpecificMethod {
	DATE_TIME {
		@Override
		public String info() {
			return DateUtil.format(new Date());
		}
	},
	CLASS_PATH {
		@Override
		public String info() {
			return System.getenv("CLASS_PATH");
		}
	},
	USERNAME {

		@Override
		public String info() {
			return "jackiezhi"+ordinal();
		}
		
	},
	VERSION {
		@Override
		public String info() {
			System.out.println(username);
			fun();
			System.out.println("username is null ??? why");
			return null;
		}
	};
	public abstract String info();
	
	public void fun(){
		System.out.println(username);
	}
	protected String username;
	
	public static void main(String[] args){
		System.out.println("-----jackiezhi-----");
		for(ConstantSpecificMethod m: ConstantSpecificMethod.values()){
			System.out.println(m.info());
		}
	}
}

对ConstantSpecificMethod.class文件进行反编译后,结果如下。

public abstract class com.zhi.learnj2.ConstantSpecificMethod extends java.lang.E
num<com.zhi.learnj2.ConstantSpecificMethod> {
  public static final com.zhi.learnj2.ConstantSpecificMethod DATE_TIME;
  public static final com.zhi.learnj2.ConstantSpecificMethod CLASS_PATH;
  public static final com.zhi.learnj2.ConstantSpecificMethod VERSION;
  static {};
  public abstract java.lang.String info();
  public static com.zhi.learnj2.ConstantSpecificMethod[] values();
  public static com.zhi.learnj2.ConstantSpecificMethod valueOf(java.lang.String)
;
  com.zhi.learnj2.ConstantSpecificMethod(java.lang.String, int, com.zhi.learnj2.
ConstantSpecificMethod);
}



发现并没有关于
DATE_TIME {
		@Override
		public String info() {
			return DateUtil.format(new Date());
		}
	},


这些代码,那么代码藏在了哪里,
D:\Code\LearnJ\bin>javap com.zhi.learnj2.ConstantSpecificMethod$1
Compiled from "ConstantSpecificMethod.java"
class com.zhi.learnj2.ConstantSpecificMethod$1 extends com.zhi.learnj2.ConstantS
pecificMethod {
  com.zhi.learnj2.ConstantSpecificMethod$1(java.lang.String, int);
  public java.lang.String info();
}


终于找到了。

你可能感兴趣的:(java)