aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Scjp Chapter5 Self Test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Scjp Chapter5 Self Test" Watch "Scjp Chapter5 Self Test" New topic
Author

Scjp Chapter5 Self Test

Srinivas Palam
Ranch Hand

Joined: Oct 20, 2005
Posts: 51


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

Joined: Mar 10, 2008
Posts: 1818
    
    7

How do statements in a switch block behave when you don't add a break ?


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Srinivas Palam
Ranch Hand

Joined: Oct 20, 2005
Posts: 51
If there is no break, it will traverse through all the case statements.
Srinivas Palam
Ranch Hand

Joined: Oct 20, 2005
Posts: 51
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

Joined: Mar 10, 2008
Posts: 1818
    
    7

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

Joined: Nov 04, 2012
Posts: 2

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

Joined: Nov 05, 2012
Posts: 1
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

Joined: Nov 04, 2012
Posts: 2

Hi Elmira, Thank you very much for the explanation. Now I understood how it is working
Rahul mir
Ranch Hand

Joined: Oct 31, 2012
Posts: 38

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

Joined: Mar 10, 2011
Posts: 3
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scjp Chapter5 Self Test