Hi there,
I wonder why this will not compile:
public class X {
static {
throw new NullPointerException();
}
}
About Question enthuware.ocajp.i.v8.2.1031 :
Moderators: Site Manager, fjwalraven
-
- Posts: 3
- Joined: Thu Feb 16, 2017 2:47 am
- Contact:
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
It should compile. The answer should be changed to ExceptionInInitializerError.
Fixed.
thank you for your feedback!
Paul.
Fixed.
thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 11
- Joined: Wed Dec 07, 2016 3:21 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
Hi, probably this question is ambiguous - in fact the Oracle compiler 1.8.0_121 for Linux refuses to compile the code because of the line "throw new NullPointerException();":
or
The quoted code snippets are compiled by aforementioned compiler without any errors, and they cause the expected ExceptionInInitializerError at run time.
This ambiguity probably could be removed if the code were changed for example as follows:X.java:2: error: initializer must be able to complete normally
static {
^
1 error
Code: Select all
public class X {
static {
problem();
}
static void problem() { throw new NullPointerException(); }
}
Code: Select all
public class X {
static String str;
static {
int len = str.length();
}
}
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
You are right. I was mistaken. I am not sure why I thought it should compile. It doesn't compile on Windows compiler either. The original answer was correct. I have changed it back.
X.java:2: initializer must be able to complete normally
static{
^
1 error
thank you for your feedback!
Paul.
X.java:2: initializer must be able to complete normally
static{
^
1 error
thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 25 guests