Bug 41945 - Java Compiler: problem with static members initialization
Summary: Java Compiler: problem with static members initialization
Status: RESOLVED DUPLICATE of bug 37565
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 critical (vote)
Target Milestone: 3.0 M1   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-08-25 17:40 EDT by Sasa Zivkov CLA
Modified: 2003-08-28 12:15 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sasa Zivkov CLA 2003-08-25 17:40:32 EDT
Hi,

When the example given bellow is compiled with the Java compiler from
the Eclipse 3.0 M2 then the static members in MyEnum class do not get 
initialized i.e. the last line prints "null".
When compiled with the standard java compiler from JDK (tried 1.3.7,
1.4.0 and 1.4.2) then everything works as expected i.e. the last line
prints out "A".
Compilers in older versions of Eclipse also work as expected.

Example:


import java.util.HashMap;
import java.util.Map;

class Enum {
	private String name;
	private static Map nameToInstance = new HashMap();
	protected Enum(String name) {
		nameToInstance.put(name, this);
		this.name = name;
	}
	public static Enum getFromName(String name) {
		return (Enum) nameToInstance.get(name);
	}
	public String toString() {
		return name;
	}
}

class MyEnum extends Enum {
	private MyEnum(String name) {
		super(name);
	}
	
	public static MyEnum A = new MyEnum("A");
}

public class TestStaticMembers {

	public static void main(String[] args) throws ClassNotFoundException {
		Class c = MyEnum.class; // now MyEnum class should be loaded 
and static members initialized 
		System.out.println(c.getName());
		Enum e = Enum.getFromName("A");
		System.out.println(e); // should print A
	}
}
Comment 1 Olivier Thomann CLA 2003-08-26 09:24:17 EDT
This is not a bug.
.class is not enough to initialize the class. The class literal is not listed in
the JLS as a case that causes a class initialization.
I will find the appropriate PR.
Comment 2 Olivier Thomann CLA 2003-08-26 09:27:29 EDT
Have a look at bug 37565. It explains the problem. We follow the JLS. Right now
javac and the JLS are not consistent. You can try with jikes and you will get
the same problem.
Comment 3 Olivier Thomann CLA 2003-08-27 10:18:29 EDT
Ok to close?
This will be revisited if the JLS are changed to add the .class as a case of
class initialization.
Comment 4 Philipe Mulet CLA 2003-08-28 12:15:30 EDT

*** This bug has been marked as a duplicate of 37565 ***