• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Possible error in question from Kathy Sierray, Bert Bates SCJP6 book

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, here is question Nr.6 from Chapter 4 (Operators):

Given:


What is the result?
A. 1;
B. 2;
C. 4;
D. 6;
E. 8;
F. Compilations fails;
G. An exception is thrown at runtime;

When i say static block

i couldn't understand which static "index" variable are they trying to initialize, and i thought that correct answer was "F. Compilation fails", but when i took a look at responses, i saw that correct answer is "C.4".
Can you help me understand why will it compile in this case?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is correct (you can try yourself). There is no static block here but an instance block which initializes the instance variable "index" to 1.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





This example tests for operators understanding. Here we have to see the nature of post increment operator.

Post increment operator increments the value of variable after evaluation.

and also we have one intializer block which is called whenever a new object of class Twisty is created. This block initialize the instance variable index by 1.

so here we have dd[index++][index++]

initially we have index=1

so dd[1][index ++] and now index =2 as we are using post increment operator.

then dd[1][2] and now index=3 as we are using post increment operator.

We have,
int [][] dd = {{9,8,7}, {6,5,4}, {3,2,1,0}};

so dd[1][2] =4

so correct option is C which is 4
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Woow! Do we have instance blocks in Java? I didn't know about that, i'm sorry. So blocks without static keywork before, are instance blocks...hmmm... but when are they parsed? Just after constructor's invocation? RIght am i?
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

PujaR Agarwal wrote:




This example tests for operators understanding. Here we have to see the nature of post increment operator.

Post increment operator increments the value of variable after evaluation.

and also we have one intializer block which is called whenever a new object of class Twisty is created. This block initialize the instance variable index by 1.

so here we have dd[index++][index++]

initially we have index=1

so dd[1][index ++] and now index =2 as we are using post increment operator.

then dd[1][2] and now index=3 as we are using post increment operator.

We have,
int [][] dd = {{9,8,7}, {6,5,4}, {3,2,1,0}};

so dd[1][2] =4

so correct option is C which is 4




Thanks for a detailed response! I just didn't try to solve it further, cause i thought that there is a compilation error at 2-nd line of code, but now it makes sense
 
PujaR Agarwal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code in an instance initializer block is executed after the constructor for the superclass is executed, and before the constructor for the class to which the initializer belongs is executed.
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What's the problem, than, with instance block?
 
PujaR Agarwal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the class definition contains a combination of instance initializer blocks in combination with the declaration of instance variables with initialization expressions, the code that comprises those items is executed in the order in which it appears in the class definition.
 
Ranch Hand
Posts: 41
Mac OS X Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well one silly thing i want to remind don't know if it makes difference or not. there may be one common option in all questions and that is compilation fails.
so always figure it out why compilation should fail. Means before checking this option you should have strong reasons for compilation fails.

Thanks & Regards
Jeetendra..!!
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I understand is that, when a class is loaded (here Twisty will be loaded because there is a main inside),

a) all the instance variables are initialized to their default values [index gets a 0].
b) the static instantiation blocks now run, in order they are written.

Now when we do

inside main(),
c) the Twisty constructor runs and calls the super constructors,
d) after all the super constructors have run, its time for the initializer blocks like those on line two to run, in order.
e) when the block at line 2 runs, it already knows about the index variable (from step a) and initializes it to 1.
f) no more instance blocks, so the contructor can now complete.

Now it sees,
go();
where line 9 simply becomes


...and 4 is printed.

Ppl, do correct me if I am wrong.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic