This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Jxam Question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Jxam Question?" Watch "Jxam Question?" New topic
Author

Jxam Question?

JM Beauvais
Greenhorn

Joined: Dec 02, 2003
Posts: 1
The Jxam asks if the following is valid:
int q;
for (int p = 0, q = 0; p < 5; p++){
System.out.println("Val = " + p + q);
}
The provided answer is: "Answer 4 is incorrect, because you can't mix the initialisation of a local loop variable declaration in the 'for' loop, with the initialisation of another loop variable."
I know that it is illegal to mix declarations/initializations in the first part of the for loop, BUT-- it seems to me that int p=0, q=0 is a valid declaration/initialization, therefore the q in the for statement is a seperate/different more local q? is that true?
jb.
nivas rao
Greenhorn

Joined: Dec 01, 2003
Posts: 9
No it's not valid. gives compile error q already declared.
int p=0,q=0; implies declaration and intialisation of two variables p,q.
mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
What I think is, q is already defined and so it cannot act as local variable for the 'for'loop at the same time when it is member variable for the class.
If we try to run the above program, we get the same compile time error that variable g is already defined.
About second part of your question, I think we cannot declare the same variable as member variable and local variable at the same time.
But we can have n number of declarations in teh first part of the 'for' loop separating them from the conditional expression using ';'.
I hope this helps!
Bye.
mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
What I think is, q is already defined and so it cannot act as local variable for the 'for'loop at the same time when it is member variable for the class.
If we try to run the above program, we get the same compile time error that variable q is already defined.
About second part of your question, I think we cannot declare the same variable as member variable and local variable at the same time.
But we can have n number of declarations in teh first part of the 'for' loop separating them from the conditional expression using ';'.
I hope this helps!
Bye.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jxam Question?
 
Similar Threads
Jxam
jxam
Jxam
Jxam
Jxam