This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Constructor and Static initialization order

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {
A() {System.out.print("CA ");}
static {System.out.print("SA ");}
}
class B extends A {
B() {System.out.print("CB ");}
static {System.out.print("SB ");}
public static void main (String[] args) {B b = new B();}
}
What is the result of attempting to compile and run the above program?
a. Prints: SA CA SB CB
b. Prints: SA SB CA CB
c. Prints: SB SA CA CB
d. Prints: SB CB SA CA
e. Runtime Exception
f. Compiler Error
g. None of the above

Answer given is b..
I am infering Static initializer block of a class gets invoked before the
constructor.Could anybody clarify on this..

When is non static initialiser block invoked?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I think this article will explain you all: http://radio.javaranch.com/corey/2004/04/19/1082390724000.html

Shawnne
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SASBCACB is the output.

The reason is that static block is executed first. If you have derived class then following sequence will take place.

1. static initilizer block of super class
2. static initilizer block of child class.
3. instance initilizer of super class.
4. rest of the super class constructor
5. instance initilizer of child class.
6. rest of the child class constructor.

For your better understanding, I have written another code snippet.

public class TechnoSample extends A
{
TechnoSample() {System.out.print("CB ");}
static {System.out.print("SB ");}
public static void main (String[] args)
{TechnoSample b = new TechnoSample();}
{System.out.print("YY");}
}


class A {
A() {System.out.print("CA ");}
static {System.out.print("SA ");}
{System.out.print("XX");}
}


here the output is, SA SB XX CA YY CB.

I hope it is very much clear.

regards,
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic