• 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

OCP Practice Exams Sef-Assessment Test 2 Q7

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code fragment below :



I have the following questions:

1. Is this line {s+= "i ";} a static block in the code?

2. Why must the s2 String variable in the Go class be static ? I noticed when I remove the static keyword, it complains in the super() method.

3.I was expecting it to print out "-s4 sb i" but instead it printed out "-sb s4 i" why is this so? Could anyone explain to me?

Thanks
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be more explicit about the source: K&B, practice exams, self assessment test #2.

1) No, it is not a static block. You can tell because the keyword static does not appear immediately before it.

2) This code snippet shows the flow of initializers (which run near when the instance variables are set up) and constructors:


The output of a main method that says new Sub() is:
super instance
super constructor
sub instance
sub constructor

This shows why removing static is a problem. The subclass' instance variable does not exist when the superclass is run. That happens later after the superclass constructor completes

3) static initializers don't run in the same order as instance initializers do because they do not depend on an instance of the class existing.
 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:

1) No, it is not a static block. You can tell because the keyword static does not appear immediately before it.
.



So, what does the block mean in this code, why is the syntax written this way instead of this ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry joe wrote:
So, what does the block mean in this code, why is the syntax written this way



Well, it's an instance initializer -- which is executed (in textual order) along with initializations of instance variables.

henry joe wrote:
instead of this ?



Try it. Does it compile? Can you figure out what it doesn't?

Henry
 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

henry joe wrote:
So, what does the block mean in this code, why is the syntax written this way



Well, it's an instance initializer -- which is executed (in textual order) along with initializations of instance variables.


Please, I am just so curious about this instance initializer. I would, finally, like to know why it was used in the code fragment? I never come across such syntax until now. So, what is the use?

Thank

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry joe wrote:
Please, I am just so curious about this instance initializer. I would, finally, like to know why it was used in the code fragment? I never come across such syntax until now. So, what is the use?

Thank



The purpose of an instance initializer, is to initialize instance members.

http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic