About Question enthuware.ocajp.i.v7.2.1326 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

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

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by admin »

I don't see any contradiction in the explanation given to Zoryanat and the explanation that you've quoted.
When you say, "might not work", what exactly do you mean by "work"? Do you mean will not compile or will not run? and what part exactly do you think will not work?

-Paul.
If you like our products and services, please help us by posting your review here.

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by ElizabethCM »

Hello,

I have read that static initialization blocks are run first. Can you please explain me what happens when running the code in this related question?
Which one of the following two:
- static int[] x = new int[0];
and
- static{
x[0] = 10;
}
is evaluated first?

Can someone explain the order in which Java evaluates the two?

I hope you guys can understand my question:
I know that the static initializations are evaluated in the same order as they're declared. I have tried to put the above lines backwards and I have seen that it does not compile. Does this mean they have the same priority when the program runs? The static variable initializations and static blocks? They're just evaluated first when a program runs, in the order they are declared?

Thanks

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

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by admin »

The order of initialization of a class is:
1. All static constants, variables and blocks.(Among themselves the order is the order in which they appear in the code.)
2. All non static constants, variables and blocks.(Among themselves the order is the order in which they appear in the code.)
3. Constructor.

If you put the two statements in reverse order, then the rule about forward reference of class fields applies. This is given in section 8.3.3 of JLS. You are trying to use the variable before it is declared.

Please run the code with the changes and see what the compiler message says.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Sergiy Romankov
Posts: 31
Joined: Thu Feb 19, 2015 8:25 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by Sergiy Romankov »

Hallo, I found a mistake

any exception thrown in a static block is wrapped into ExceptionInInitializerError

Not any exception but only Runtime Exceptions

Thanks, for your work.
Sergey

Sergiy Romankov
Posts: 31
Joined: Thu Feb 19, 2015 8:25 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by Sergiy Romankov »

if there is checked Exception in static block we will get compilation error

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

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by admin »

Sergiy Romankov wrote:Hallo, I found a mistake

any exception thrown in a static block is wrapped into ExceptionInInitializerError

Not any exception but only Runtime Exceptions

Thanks, for your work.
Sergey
It is not a mistake. It says "any exception thrown", which means it is talking about exceptions that can be thrown in the first place. But I agree that it would be good to add this information in the explanation to make it clear.
If you like our products and services, please help us by posting your review here.

Ingvarr
Posts: 7
Joined: Sun May 08, 2016 8:09 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by Ingvarr »

The explanation says: "If you try to throw a checked exception from a static or instance initializer block to the outside, the code will not compile."
That much is true.
However, by talking about *checked* exceptions, the sentence seems to imply (at least, in my eyes) that it is permissible to throw an RTE, as in the following code:

class Class{
static { throw new RuntimeException(); } // is to be commented out to make the next statement reachable
{ throw new RuntimeException(); }
}

The compiler isn't happy with that, either; it still wants the initializer "be able to complete normally." IMHO, the wording should be changed to reflect the fact that no exceptions at all may be thrown from an initializer block, be it static or otherwise, to the outside deliberately.

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

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by admin »

Ingvarr wrote:However, by talking about *checked* exceptions, the sentence seems to imply (at least, in my eyes) that it is permissible to throw an RTE, as in the following code:

class Class{
static { throw new RuntimeException(); } // is to be commented out to make the next statement reachable
{ throw new RuntimeException(); }
}

The compiler isn't happy with that, either; it still wants the initializer "be able to complete normally." IMHO, the wording should be changed to reflect the fact that no exceptions at all may be thrown from an initializer block, be it static or otherwise, to the outside deliberately.
No, that is not correct. The following compiles fine:

Code: Select all

static { 
  if(someCondition) throw new RuntimeException();  //compiles
}
and the following doesn't:

Code: Select all

static { 
  if(someCondition) throw new Exception(); //does not compile
}
It is true that the code that you've given doesn't compile. But the reason that you gave for that is incorrect. The problem is not that the code is throwing an exception "deliberately". The problem is that the code *always* throws an exception. If the static (or instance) initialiser always throws an exception, you can't load a class at all (or create objects of it). The situation is similar situation to "unreachable code". That is why the compiler refuses to compile.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Ingvarr
Posts: 7
Joined: Sun May 08, 2016 8:09 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by Ingvarr »

admin wrote: If the static (or instance) initialiser always throws an exception, you can't load a class at all (or create objects of it). The situation is similar situation to "unreachable code". That is why the compiler refuses to compile.
Ah! I see the light now :) Thanks!

mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by mjmsausava »

Just to be sure.. the following program will throw exception because the array did not exist when the static block ran?

class AX{
static int[] x = new int[0];
static{
x[0] = 10;
}
public static void main(String[] args){
AX ax = new AX();
}
}

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

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by admin »

What happened when you tried to compile and execute it?
If you like our products and services, please help us by posting your review here.

swarna pakeer
Posts: 16
Joined: Thu Mar 19, 2020 2:27 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by swarna pakeer »

as per book OCAJP by Hanuman deshmukh,page no-285, it says we cannot throw checked exceptions from static initializer but instance initializers are allowed to throw checked exceptions in this case we should declare an exception thrown from an instance initializer in the throws clause of each constructor of the class. please clarify

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

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by admin »

Not sure what you mean by clarify. The statement is quite clear itself. What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

swarna pakeer
Posts: 16
Joined: Thu Mar 19, 2020 2:27 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by swarna pakeer »

what i mean is the explanation in the question says "If you try to throw a checked exception from a static or instance initializer block to the outside, the code will not compile." but the book says we can throw checked exceptions from the instance initializer if we declare it in all the constructors throws clause .since there is contraction between two statements i asked for an explanation .

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

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by admin »

The book is correct as shown by the following short code:

Code: Select all

public class TestClass{

  {
    if(true) throw new Exception(); //compiles
  }

  static {
    if(true) throw new Exception(); //does not compile
  }

  TestClass() throws Exception{ }
}
The explanation should be enhanced with this detail.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

sijucm
Posts: 8
Joined: Tue Jan 11, 2022 4:41 am

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by sijucm »

And, if it is not the static block but a non-static block, the error is ArrayIndexOutOfBoundsException. So, does Oracle or others expect us to know that? How does it benefit anybody?

class Ax {
int [] x = new int[0];
{
x[0] = 1;
}
}

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

Re: About Question enthuware.ocajp.i.v7.2.1326 :

Post by admin »

Hi Sijucm,
Our job is to prepare you for the exam. We we get into the argument of what is and what is not useful, we will not be doing that job well. But I understand your concern and rest assured that you are not alone. Many people have the same complaint. The right forum to raise your concern might be twitter or some other social media network.
Here, we try to keep the discussion focused on the task at hand, i.e., learning what we need to learn for passing the exam.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 45 guests