Forums Register Login

Daught

+Pie Number of slices to send: Send
Hello there. I was looking around this site, and saw the following code from a previous thread:

class A{
static{
int x = 5;
}
static int x,y;
public static void main(String args){
doStuff();
System.out.println(y++ + x + ++x);
}
static void doStuff(){
x++;
++y;
}
}

But I didn�t understand this part:

static{
int x = 5;
}

what�s that? It isn�t a constructor, and not a method, and no inner class. So what can it be?

Thanks.
+Pie Number of slices to send: Send
Hi Rafeal,

They are initializer blocks. This one above is a static initializer block.

Regards,
Jothi Shankar Kumar. S
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
Henry,
sorry about that, I knew that you had a policy,but I changed the name and didn�t remember that. I will change right away.

Owen,
thanks for the link. I don�t remember reading about this on the SCJP book,
but I think it must be there somewhere.

cya!
+Pie Number of slices to send: Send
Also, keep in mind...

static{
int x = 5;
}

is not the variable that will be used in main or doStuff(), the x variable that is used will be initialized to 0 (thus resulting in an answer of 4).
Not so fast naughty spawn! I want you to know about
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 678 times.
Similar Threads
SCJP Chapter 01 Self Test Qn no. 3
Differance in int[](array) and int...(var args)
code explanation needed
Boxing/Unboxing
Wrappers
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:17:05.