• 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

static initializer in sub class and super class

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai
this question is from Dan Chisholm's mock exam

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();
}}

Answer is, it prints SA SB CA CB
I got the same when I compile and run the code.
can any one explain me,why the output is not SA CA SB CB
Thanks in advance
Haripriya
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haripriya,

Thank you for using my exams.

Static initialization of class B must be completed before the main method can run; SA SB is printed even before the main method runs. CA CB is printed when the code contained in the body of the main method creates an instance of class B.
 
harry psll
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Dan for the reply.
it helped me.

haripriya
 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic