🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #16531

Java Bug or tough certification exam question for you concerning static initializers

#16531
From: Wido Riezebos
Date: Wed, 10 Feb 1999 00:00
52 lines
1570 bytes
Consider the following:

class  A
{
  static String str = new String("init: ");
  static { str += "a";
         }
}

class  B extends A
{ static { str += "b";
         }
}

public class Test
{ public static void main(String[] args)
 { B instOfB = new B();
    System.out.println(B.str);
  }
}

This will print

init: ab

as you would expect.

But if you comment out the B instOfB = new B(); i.e. the main looks like

{ public static void main(String[] args)
 { //B instOfB = new B();
    System.out.println(B.str);
  }

this will print

init: a

Now here's the question: WHY DOES'NT THE SECOND STATIC INITIALIZER FIRE?? Is
it because the jvm decides it does not need to load the B class since it
thinks the str is defined in A and B is an A?

The certification question: (multiple multiple choice)
1) This is a Bug
2) This is a Feature
3) This is Not Defined
4) Some of the above
5) I need some asperine

I am looking forward to your answer (especially the one from Goshling)

Wido Riezebos
Technical Details
Message-ID: <79rmjc$99p15@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,1512ce46783b168a,start
X-Google-Attributes: gidf74b3,public
From: "Wido Riezebos" <[REMOVE_THIS]w.riezebos@itude.com>
Subject: Java Bug or tough certification exam question for you concerning static initializers
Date: 1999/02/10
Message-ID: <79rmjc$99p15@forums.borland.com>
X-Deja-AN: 442750361
Organization: Another Netscape Collabra Server User
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4
Newsgroups: borland.public.jbuilder.java.language