About Question enthuware.ocajp.i.v8.2.1031 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
atrofimov
Posts: 3
Joined: Thu Feb 16, 2017 2:47 am
Contact:

About Question enthuware.ocajp.i.v8.2.1031 :

Post by atrofimov »

Hi there,
I wonder why this will not compile:
public class X {
static {
throw new NullPointerException();
}
}

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Post by admin »

It should compile. The answer should be changed to ExceptionInInitializerError.
Fixed.
thank you for your feedback!
Paul.

jankur
Posts: 11
Joined: Wed Dec 07, 2016 3:21 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Post by jankur »

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();":
X.java:2: error: initializer must be able to complete normally
static {
^
1 error
This ambiguity probably could be removed if the code were changed for example as follows:

Code: Select all

public class X {
static {
problem();
}
static void problem() { throw new NullPointerException(); }
}
or

Code: Select all

public class X {
static String str;
static {
int len = str.length();
}
}
The quoted code snippets are compiled by aforementioned compiler without any errors, and they cause the expected ExceptionInInitializerError at run time.

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Post by admin »

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests