Understanding enum's static members initialization

This question already has an answer here: Execution order of of static blocks in an Enum type wrt to constructor 3 answers The enum's constants are themselves initialized by calling the constructor. This means the constructor cannot access the constant since it's not yet created at that time. In other words, say you have: enum MyEnum { FOO, BAR; private MyEnum() {

了解枚举的静态成员初始化

这个问题在这里已经有了答案: 将Enum类型wrt中的静态块的执行顺序返回给构造方法3 枚举的常量是通过调用构造函数初始化的。 这意味着构造函数不能访问该常量,因为此时尚未创建该常量。 换句话说,假设你有: enum MyEnum { FOO, BAR; private MyEnum() { // Illegal // FOO already calls this constructor System.out.println(FOO); } } FOO和BAR相当于: public static final

Can an enum have a constructors for each of its constants

Please look at this link. In his book Effective Java, Joshua Bloch says Note that the Operation constants are put into the stringToEnum map from a static block that runs after the constants have been created. Trying to make each constant put itself into the map from its own constructor would cause a compilation error . This is a good thing, because it would cause a NullPointerException if

一个枚举可以为每个常量有一个构造函数

请看这个链接。 Joshua Bloch在他的着作Effective Java中写道 请注意,操作常量从创建常量后运行的静态块放入stringToEnum映射中。 试图让每个常量都将 其自己的构造函数放入映射中会导致编译错误 。 这是一件好事,因为如果它合法的话会导致NullPointerException。 除编译时常量字段外,枚举构造函数不允许访问枚举的静态 字段。 此限制是必需的,因为这些静态字段在构造函数运行时尚未初始化。 我有两个问题

Enum and static variable in constructor

Access to static fields in enum constructor is forbidden by the compiler. The source code below works, it uses a static field: public enum TrickyEnum { TrickyEnum1, TrickyEnum2; static int count; TrickyEnum() { incrementCount(); } private static void incrementCount() { count++; } public static void main(String... args) { System

枚举和静态变量在构造函数中

编译器禁止访问枚举构造函数中的静态字段。 下面的源代码工作,它使用一个静态字段: public enum TrickyEnum { TrickyEnum1, TrickyEnum2; static int count; TrickyEnum() { incrementCount(); } private static void incrementCount() { count++; } public static void main(String... args) { System.out.println("Count: " + count); } } 输出

Accessing static fields inside enum

Generally I have learnt that static blocks/initializations inside a Java class get executed first during compilation. But trying to access the static field inside the enum gives me the error Cannot refer to the static enum field ExportToReports.Animal.num within an initializer. Why does this error occur as the static variable must have been initialised? public enum Animal{ cat(2), dog(3

在枚举中访问静态字段

通常我已经了解到,在编译期间,Java类中的static块/初始化会首先执行。 但试图访问enum内的静态字段给我的错误 在初始化程序中不能引用静态枚举字段ExportToReports.Animal.num 。 为什么会发生此错误,因为静态变量必须已经初始化? public enum Animal{ cat(2), dog(3); int id, number; static int num = 5; Animal(int id) { this.id = id; this.number = Animal.num; } }

Why enum constructor can't access static field

Possible Duplicate: Why can't enum's constructor access static fields? enum Test { e1,e2; int i=0; static int j=5; Test(){ System.out.println(i+" "+j); } } In the above code the constructor can access the instance variable but not the static variable J. I have read the answer relate to other author all are saying the e1 and e2 initialized before the initializat

为什么枚举构造函数不能访问静态字段

可能重复: 为什么不能枚举的构造函数访问静态字段? enum Test { e1,e2; int i=0; static int j=5; Test(){ System.out.println(i+" "+j); } } 在上面的代码中,构造函数可以访问实例变量,但不能访问静态变量J. 我已经阅读了与其他作者有关的答案,都说e1和e2在J(静态字段)的初始化之前被初始化了,但是根据Java规范,所有的静态字段都会在类加载到内存时初始化,也就是在运行之前构造函数。

Are there any guarantees in JLS about order of execution static initialization blocks?

I wonder if it's reliable to use a construction like: private static final Map<String, String> engMessages; private static final Map<String, String> rusMessages; static { engMessages = new HashMap<String, String> () {{ put ("msgname", "value"); }}; rusMessages = new HashMap<String, String> () {{ put ("msgname", "значение"); }}; } priv

JLS中有关于执行静态初始化块顺序的任何保证吗?

我想知道使用像下面这样的结构是否可靠: private static final Map<String, String> engMessages; private static final Map<String, String> rusMessages; static { engMessages = new HashMap<String, String> () {{ put ("msgname", "value"); }}; rusMessages = new HashMap<String, String> () {{ put ("msgname", "значение"); }}; } private static Map<S

Changing XLSX form control location with Apache POI

I've some number of xlsm files containing form controls. I'd like to programmatically move a particular button down a few rows on each sheet. My first hope was to do something like this: FileInputStream inputStream = new FileInputStream(new File("t.xlsm")); XSSFWorkbook wb = new XSSFWorkbook(inputStream); XSSFSheet xs = (XSSFSheet)wb.getSheetAt(1); RelationPart rp = xs.getRelationParts

使用Apache POI更改XLSX表单控制位置

我有一些包含表单控件的xlsm文件。 我想以编程方式将每个工作表上的特定按钮向下移动几行。 我的第一个希望是做这样的事情: FileInputStream inputStream = new FileInputStream(new File("t.xlsm")); XSSFWorkbook wb = new XSSFWorkbook(inputStream); XSSFSheet xs = (XSSFSheet)wb.getSheetAt(1); RelationPart rp = xs.getRelationParts().get(0); XSSFDrawing drawing = (XSSFDrawing)rp.getDocumentPart(); for(XSSFS

In my application not getting proper row number or value .(Apache poi)

In this application,I am reading xlsx file by using apache-poi ,but I am not getting only row number , it is giving some other data also, please check my code and output. This is my code: public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { // c => cell System.out.println("The Value of Row is : "+attribu

在我的应用程序中没有得到正确的行数或值(Apache poi)

在这个应用程序中,我通过使用apache-poi来读取xlsx文件,但是我没有获取行号,它也提供了其他一些数据,请检查我的代码并输出。 这是我的代码: public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { // c => cell System.out.println("The Value of Row is : "+attributes.getValue("r")); if(name.equals("c")) {

Reading excel files .xlsx via Java

So my excel file is relatively small in size. It contains 8 sheets. Each sheet has "records" of data which i need to read. Each sheet also has the first row reserved for headers which i skip; so my data will begin from the 2nd row (1st index) of each sheet and end on the last record. So, below is my code to iterate through the sheets and read each row however it fails to read each

通过Java读取excel文件.xlsx

所以我的excel文件的大小相对较小。 它包含8张。 每张表都有我需要阅读的数据的“记录”。 每张表还有第一行保留给我跳过的标题; 所以我的数据将从每张纸的第二行开始(第一个索引)并结束于最后一个记录。 所以,下面是我的代码遍历表单并读取每一行,但它无法读取每张表。 我似乎无法弄清楚为什么。 请看看,任何建议将不胜感激。 谢谢! FileInputStream fis = new FileInputStream(new File(filePath)); XSSFWorkbook

XLSX get last column?

How can i get the the last column when reading a xlsx file using the Apache POI API? There's a getLastRowNum method but nothing related with number of columns. Thx for help. You have to check each Row and call Row.getLastCellNum() . The max cell num is the last column num.

XLSX获得最后一列?

如何在使用Apache POI API读取xlsx文件时获得最后一列? 有一个getLastRowNum方法,但与列数无关。 Thx寻求帮助。 你必须检查每一Row并调用Row.getLastCellNum() 。 最大单元号是最后一个列号。