aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Non static variable is incrementing  for method call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Non static variable is incrementing  for method call" Watch "Non static variable is incrementing  for method call" New topic
Author

Non static variable is incrementing for method call

venu gopal
Ranch Hand

Joined: Nov 21, 2000
Posts: 40
Hi All,
can you please help me in understanding this question? This is from K&B Chapter3 Q 12.
1. class BoolArray {
2. boolean [] b = new boolean[3];
3. int count = 0;
4.
5. void set(boolean [] x, int i) {
6. x[i] = true;
7. ++count;
8. }
9.
10. public static void main(String [] args) {
11. BoolArray ba = new BoolArray();
12. ba.set(ba.b, 0);
13. ba.set(ba.b, 2);
14. ba.test();
15. }
16.
17. void test() {
18. if ( b[0] && b[1] | b[2] )
19. count++;
20. if ( b[1] && b[(++count - 2)] )
21. count += 7;
22. System.out.println("count = " + count);
23. }
24. }
what is the result?
A. count = 0
B. count = 2
C. count = 3
D. count = 4
E. count = 10
F. count = 11
Here count is not static variable. How it is incrementing for each call to set() method?
Prasad Kuppa
Ranch Hand

Joined: Apr 01, 2002
Posts: 151
Because, you are operating on same object..
[ July 30, 2003: Message edited by: Prasad Kuppa ]
venu gopal
Ranch Hand

Joined: Nov 21, 2000
Posts: 40
Thank you Prasad
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Venu,
answer is 3. right?
Barkat Mardhani
Ross Goldberg
Ranch Hand

Joined: Jul 09, 2003
Posts: 63
I got (c) for a value of c, like Barkat:
lines 12 and 13 increment count in line 7 by 1 each (total of 2)
The first if in line 18 brings count to 13 after resulting to true
(true and (false or true)) (remember && has precedence over || so it goes in there and no short-circuiting occurs), thus ending in a value of 3.
The next if in line 20 gets short-circuited after the first operand results in false.
1. class BoolArray {
2. boolean [] b = new boolean[3];
3. int count = 0;
4.
5. void set(boolean [] x, int i) {
6. x[i] = true;
7. ++count;
8. }
9.
10. public static void main(String [] args) {
11. BoolArray ba = new BoolArray();
12. ba.set(ba.b, 0);
13. ba.set(ba.b, 2);
14. ba.test();
15. }
16.
17. void test() {
18. if ( b[0] && b[1] | b[2] )
19. count++;
20. if ( b[1] && b[(++count - 2)] )
21. count += 7;
22. System.out.println("count = " + count);
23. }
24. }
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Non static variable is incrementing for method call
 
Similar Threads
Short Circuit operators and Bitwise operators
Operator precedence and associativity confusion........
Question from K&B Book
plz Explain me line by line this code
I am a real JAVA beginner, please take a look at this code.