• Post Reply Bookmark Topic Watch Topic
  • New Topic

static initializer code blocks  RSS feed

 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am experimenting with some code to see what will cause a static code block to run. In the source file, I have three classes: Cup, Cups and ExplicitStaticInitialization. As I understand, static initializers are executed only once, either the first time you make an object of the class, or the first time you access a static member of that class.

I am trying to find out the reason why the static initializer does not run on the lines (2) and (3) of the code below.

The code at line (1) makes it run. But even though, (2) and (3) are outside main() shouln't they cause it to run also?

Can anyone shed some light on this?

Thoughts appreciated.


class Cup {
Cup(int marker) {
System.out.println("Cup("+marker+")");
}
void f(int marker) {
System.out.println("f("+marker+")");
}
}

class Cups {
static Cup c1;
static Cup c2;
static {
c1 = new Cup(1);
c2 = new Cup(2);
System.out.println("in static code block");
}
}

public class ExplicitStaticInitialization {

public static void main(String[] args) {
System.out.println("inside main()");
// Cups.c1.f(99); // this causes the static code block to run
// Cups a = new Cups(); // this does too (1)
}

// static Cups x = new Cups(); // does
// static Cups y = new Cups(); // does
// Cups z = new Cups(); // does NOT. Why? (2)

static Cups z1; // doesn't because the Object is initialised to null
private Cups z2; // doesn't because the Object is initialised to null

// private static Cups z4 = new Cups(); // does
public static Cups z4 = new Cups(); // does
public Cups z3 = new Cups(); // does NOT. Why? (3)
}
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, don't forget to use the code tags when posting code, for readability.

You wrote it yourself: "static initializers are executed only once, either the
first time you make an object of the class, or the first time you access a
static member of that class"

Look at the two lines you can't understand:

They are essentially the same, by the way. Both are declaring non-static
fields, it's just that the first has default access and the second public.
Anyway, the reason that these line don't trigger the static initializer is
that you aren't instantiating the class, so you aren't executing their
initialized (right-hand sides). See what happens when you put the following
in your main.
 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the lines at (2) and (3) don't cause the static initializer to run, because since they aren't static, they themselves aren't run completely until I create an instance of the ExplicitStaticInitialization class.

I think I've got it now. Thank you Jeff!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!