Forums Register Login

SCJP help

+Pie Number of slices to send: Send
Hi Guys, I have one small question with the following code:

1. class Banana {
2. int x = 1;
3. public static void main(String [] args) {
4. int x = 2;
5. Banana b = new Banana();
6. b.go();
7. }
8. { x += x; } // What is this?
9. void go() {
10. ++x;
11. System.out.println(x);
12. }
13. }


What is the result?
+Pie Number of slices to send: Send
Unmarked braces (line 8 in this code) denote "initializer blocks," which execute when the class is initialized. In this case, when an instance of Banana is created, the instance variable x (not the local variable x) is first initialized to 1, but then the initializer code increments it to 2.

Note: Braces preceded with "static" are "static blocks," which execute when the class is loaded.
+Pie Number of slices to send: Send
Also instance intialization is called after super class construction

for ex

class a
{
int a;

{
a=10;
}
a()
{
super()
// Now instance intializations will be called in top - down orders

}

static public void main(strings args[])
{
new a();
}

}
And inside of my fortune cookie was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 736 times.
Similar Threads
object ref assignment conversion
going bananas with this one
Why Byte boxing doesn't work here
Please help with these questions
E practice exam questions for SCJP 5
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 00:37:19.