Home > Java > javaTutorial > A simple example of using reflection to obtain static variable names and variable values ​​​​in Java classes

A simple example of using reflection to obtain static variable names and variable values ​​​​in Java classes

高洛峰
Release: 2017-01-24 14:33:58
Original
2072 people have browsed it

JAVA can obtain the names of member variables and static variables through reflection, but local variables are unlikely to be obtained.

public class Test {
 
  public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    //获取所有变量的值
    Class clazz = Class.forName("com.qianmingxs.ScoreTable");
    Field[] fields = clazz.getFields();
 
    for( Field field : fields ){
      System.out.println( field.getName() + " " +field.getInt(clazz) );
    }
    //获取单个变量的值
    /*Class clazz = Class.forName("com.qianmingxs.ScoreTable");
    Field field = clazz.getField("FIVE");
    System.out.println( field.getInt(clazz));*/
  }
 
}
Copy after login

The Java class to be viewed is:

public class ScoreTable {
  private ScoreTable() {}
  public final static int FIVE = 100;//成5
  public final static int DOUBLE_ALIVE_FOUR = 99;//双活4(分析对手用)
  public final static int ALIVE_FOUR_AND_DEAD_FOUR = 98;//活4死4(对手分析用)
  public final static int ALIVE_FOUR_AND_ALIVE_THREE = 96;//活4活3(分析对手用)
  public final static int ALIVE_FOUR_AND_DEAD_THREE = 95;//活4死3(分析对手用)
  public final static int ALIVE_FOUR_AND_ALIVE_TWO = 94;//活4活2
  public final static int ALIVE_FOUR = 93;//活4
  public final static int DOUBLE_DEAD_FOUR = 92;//双死4
  public final static int DEAD_FOUR_AND_ALIVE_THREE = 91;//死4活3
  public final static int DEAD_FOUR_AND_ALIVE_TWO = 90;//死4活2
  public final static int DOUBLE_ALIVE_THREE = 80;//双活3
  public final static int ALIVE_THREE_AND_DEAD_THREE = 70;//活死3
  public final static int HALF_ALIVE_FOUR = 65;//半活4(类似○○ ○形),优先级小于活4
  public final static int ALIVE_THREE = 60;//活3
  public final static int DEAD_FOUR = 50;//死4
  public final static int DOUBLE_ALIVE_TWO = 40;//双活2
  public final static int DEAD_THREE = 30;//死3
  public final static int ALIVE_TWO = 20;//活2
  public final static int DEAD_TWO = 10;//死2
  public final static int SINGLE = 0;//单个
}
Copy after login

The above exploit The simple example of using reflection to obtain the static variable name and variable value in the Java class is all the content shared by the editor. I hope it can give you a reference, and I hope you will support the PHP Chinese website.

For more simple examples of using reflection to obtain static variable names and variable values ​​​​in Java classes, please pay attention to the PHP Chinese website for related articles!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template