• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scjp Chapter5 Self Test

 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can someone please explain why it is printing s value as 9 10 in the first iteration instead of 9 only

D:\Java>java Ebb
X value is 9
S value is


hello 9 10
X value is 10
S value is 9 10


hello 9 10 10
X value is 11
S value is 9 10 10


hello 9 10 10 d 13

D:\Java>
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do statements in a switch block behave when you don't add a break ?
 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is no break, it will traverse through all the case statements.
 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understood now.

D:\Java>java Ebb
Y value is 0
X value is 9
S value is
hello 9 10 --> When x is 9, case 9 and case 10 statements will be exected then break statement.


Y value is 1
X value is 10
S value is 9 10
hello 9 10 10 -- When x is 10, case 10 will be exected then break statement.


Y value is 2
X value is 11
S value is 9 10 10
hello 9 10 10 d 13 --> when x is 11, default and case13 statements will be executed and the output is9 10 10 d 13. Is that the correct assumption? Please let me know.

 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right, you have to be aware of the fall through behavior of the switch statement with questions like these, good job
 
Nitesh Jangid
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to java. I understood this program but could you tell me the purpose of line 22 ( static { x++; } ). I didn't really get how it is printing value of X as 9. Help!
 
Elmira Pishchalnikova
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Nitesh. This is static initialization block. Static initialization block run when the class is first loaded. That is why x is not 8 as we can think but 9 because before we call method, we have x++ in initialization block .
Does it have sense for you?
 
Nitesh Jangid
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Elmira, Thank you very much for the explanation. Now I understood how it is working
 
Rahul mir
Ranch Hand
Posts: 38
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitesh Jangid wrote:Hi,
I am new to java. I understood this program but could you tell me the purpose of line 22 ( static { x++; } ). I didn't really get how it is printing value of X as 9. Help!


In java you see "static variables", "static methods", "static classes" and "static blocks".

A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword.

static {
// initialization code
}

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.
 
Kisor Biswal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul mir wrote:
Nitesh Jangid wrote:Hi,
I am new to java. I understood this program but could you tell me the purpose of line 22 ( static { x++; } ). I didn't really get how it is printing value of X as 9. Help!


In java you see "static variables", "static methods", "static classes" and "static blocks".

A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword.

static {
// initialization code
}

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.


Also the static blocks are executed before any static methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic