This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please explain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please explain" Watch "please explain" New topic
Author

please explain

Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
<CODE>
class Q43
{
int y=1;
Q43()
{
this(y);
}
Q43(int x)
{
y=x++ + ++x;
}
public static void main(String [] args)
{
Q43 t = new Q43();
System.out.println(t.y);
}
}
</CODE>
Why it doesnt compile?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Compiler collects the instance field initialization and instance initializers in textual order and places the resulting code inside any constructor that does'n begin with this(...). It places that code after the implicit/explicit call to super(...) but before any code that the programmer wrote in the constructor. The compiler complains because if the declaration of y was placed in the constructor that takes argument calling this(y) refers a variable that is not initialized yet.
The resulting code is like this

To check that use javap
hope it helps


SCJP2. Please Indent your code using UBB Code
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Thankx for the reply..
int[] a={11,12,13,14};
int[] b={0,1,2,3};
System.out.println(a[(a=b)[3]]);
a=b doesnt have any effect??
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Not exactly. Look at the following:

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by Michael Ernest:
[B]Not exactly. Look at the following:

[/B]

Thankx for your help mike...
But i am still having difficulty in understanding..
(a[(a=b)[3]]);
How come this gives two different outputs?
Please help
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Interesting, huh? It's because the act of assignment only changes things once. After that, a and b are the same value. But because assignment operations have a low priority, they usually evaluate after index operations, thuis leaving the first expression unaffected by its use.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
JLS 15.13.1 does wonders for explaining these kinds of expressions:

* First, the array reference expression is evaluated. If this evaluation completes abruptly, then the array access completes abruptly for the same reason and the index expression is not evaluated.
* Otherwise, the index expression is evaluated. If this evaluation completes abruptly, then the array access completes abruptly for the same reason.

Thus the evaluation of what it is inside [] can not affect to the evaluation of the array reference expression, because this has happened before. But an assingment within [] is effective to the code that is at the right of where occurs:
That is why
System.out.println(a[(a=b)[3]] * a[1]);
prints the result of 14*1
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please explain