This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Initializer blocks  RSS feed

 
Nikhil Sun
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any difference technically between the following two code initializer blocks defined in a class:

static{
int j=44;
System.out.println("in static block");
}

&

{
int j=44;
System.out.println("in initializer block");
}
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, both are completely pointless, having no lasting effect on anything other than the output messages received. In both situations you define a local variable named j, which is completely forgotten after the block exits. Aside from explaining the output messages - who cares? The local variable will be forgotten either way. The only difference is that the first code sample will print output messages only when the class is loaded, while the second will give you output every time you create a new instance of the class. Otherwise, there is no meaningful difference between the two code samples.
[ November 01, 2006: Message edited by: Jim Yingst ]
 
Nikhil Sun
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...while on static initializers what about the following snippet which I found somewhere on the net:

public class CDummy implements IDummy
{
static { // the only static block
xx = 9;
}

public static void main(String args[])
{
System.out.println("CDummy.");

System.out.println("xx = " + xx);
}

static int xx = 7;
}
A few questions on this:
1)Why no compile error because xx is used in the static block prior to even being declared?
2)Why does the above code print 7 and not 9?But if in the code we change static int xx = 7; to
static int xx; then the value 9 is printed instead of the default 0 for int which I thought would be printed.Why?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)Why no compile error because xx is used in the static block prior to even being declared?
2)Why does the above code print 7 and not 9?But if in the code we change static int xx = 7; to
static int xx; then the value 9 is printed instead of the default 0 for int which I thought would be printed.Why?


For question one, it is perfectly fine to refer to an static class variable that is declared later in the class -- the compiler does allow forward references.

Initializations of static variables, and executions of static initializers both happen during the loading of the class. The order that they are executed at from the top to the bottom of the class -- the order that they are defined.

This means, in your example, when the static initializer starts, xx has a default value of zero. This initializer changes this value to 9. Later when the variable is initialized, it will be initialized to a value of 7.

Henry
 
Nikhil Sun
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This means, in your example, when the static initializer starts, xx has a default value of zero. This initializer changes this value to 9. Later when the variable is initialized, it will be initialized to a value of 7.


Referring to my 2nd question consider the following:

If we change
static int xx = 7; to
static int xx;

In this case then why does the default int value 0 not get printed?Why is the default initialization not considered?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nikhil Sun:

Referring to my 2nd question consider the following:

If we change
static int xx = 7; to
static int xx;

In this case then why does the default int value 0 not get printed?Why is the default initialization not considered?


In the example, when the static initializer starts, xx has a default value of zero. This static initializer changes the value to 9. Later when the variable is initialized... well... nothing happens. The value doesn't change, and is still 9.


Keep in mind, that the main() method is called after the class is loaded and initialized. So it will print the last result. If you want to print the default value (zero) of xx, you'll have to do it from a static initializer that is eariler than the one that initializes it.

Henry
[ November 02, 2006: Message edited by: Henry Wong ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The sequence, in which the static block appears matters:


prints 5


prints 7


Yours,
Bu.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!