• 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

Regarding static blocks

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

This will not initialize the static code/blocks of
Super and Sub.
But if change the class "test" as below :

Output is :
Inside static : test
Does that mean when i just declare the instance of the class
in which main() exists, the whole class gets initialized,
and instance is not required to be created to execute
the static block/code

 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Initialize a type means to execute its static block/initializers.
A type is initialized at the first active use.
The first active use is the first of the following.
The class was designed as the controlling class: the one with the main method.
A subclass is initialized. Not true for interfaces.
An static member is accessed, only if not final with a value known at compile time.
Creation of an instance of a class.
Some invocations in the relective API.
So it is not because the "test t" code why test is initialized but because it contains main. No, an instance never is needed for the static blocks because executing them is the proper initalization of the class.

hope helps
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this modification to see the initialization:


[This message has been edited by marilyn murphy (edited September 22, 2001).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela,
Further to what Jose said, in your first example the statement 'Sub s' just creates a reference variable...not a 'Sub' object; the 'Super' and 'Sub' classes are not loaded.
If you add a line to directly reference a static variable, for example, <code>int i = Super.j; </code> then the 'Super' class will be loaded and the statement in 'Super' will print. You don't need an instance of 'Super' to access the class variable.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited September 22, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic