aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "output" Watch "output" New topic
Author

output

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
class Test {
static int x,y;

Test() {
this(0,0);
}
Test(int x) {
x=x++;
}
Test(int x, double y)
{
this(0);
x=x++; y=++y;
}
public static void main(String [] args) {
new Test();
new Test(6, 9);
System.out.println(x + "," + y);
}
}
What is the result?
1.Prints 0, 0
2.Prints 6, 10
3.Prints 7, 10
4.Prints 4, 2
Answer given is 1.
i feel it should be 2.
could anyone explain why


nss
Madan, Gopal
Ranch Hand

Joined: Aug 13, 2001
Posts: 86
This is an example of "pass by value" - specifically for primitives.
You have static integer variables x and y - Automatically initialized to their default primitive values - in this case to 0.
Then all you're doing is manipulate the copies of x and y inside various constructors. SCOPE of the intermediate values of x and y are confined to the respective constructors where they are being manipulated.
The moment you come out of all your constructors, back to the main method and call
System.out.println(x + "," + y);
x and y are getting the ORIGINALLY INITIALIZED default values
Hence x = 0 and y = 0.
To confirm, in the declaration of the static int variables x and y, you can explicitly initialize them to any value you want. Those are the exact values you will get in the output.
For e.x. if you say:
static int x = 14, y = 23;
your output will be 14,23
lu v thuan
Ranch Hand

Joined: Oct 25, 2001
Posts: 32
Hi Sawant:
Why the program printed 0,0
That is local var x and y hide class var x and y
For example to println 6 ,0 You should use keyword this
Test(int x, double){
this.x = x++;//asign local var x value to class member's x,
//then increment local var x by 1
y=++y;//increment local var y by 1 and then asign to local var
// y, so class member's y is still = 0;
}
Hope this help
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by Madan, Gopal:
This is an example of "pass by value" - specifically for primitives.
You have static integer variables x and y - Automatically initialized to their default primitive values - in this case to 0.
Then all you're doing is manipulate the copies of x and y inside various constructors. SCOPE of the intermediate values of x and y are confined to the respective constructors where they are being manipulated.
The moment you come out of all your constructors, back to the main method and call
System.out.println(x + "," + y);
x and y are getting the ORIGINALLY INITIALIZED default values
Hence x = 0 and y = 0.
To confirm, in the declaration of the static int variables x and y, you can explicitly initialize them to any value you want. Those are the exact values you will get in the output.
For e.x. if you say:
static int x = 14, y = 23;
your output will be 14,23

Hi madan...
I have a clarification. I believe it is not pass by value issue.Its due to variable hiding.
The static variables x and y are not touched anywhere inside the constructor.To touch or manipulate it must be this.x = x++; same for y also. But the problem here is if we do this.y = ++y; //It wont compile(becoz of int = double //wont cut it)
so what we get as an output is mearly the intiliazed value of the static variables
Feel free to correct me if i am wrong
Thankyou
Ragu
Madan, Gopal
Ranch Hand

Joined: Aug 13, 2001
Posts: 86
Thanks Ragu for correcting me.
I realize the fact and was aware of it but had tough time retaining it in my mind.
Hopefully, this time around it will stay.
Thanks again
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Thanx alot Madan ,Lu, & Raghu,
Actually i was under the impresion that static variables are visible throught the class and changes made on them is affected
but i failed to see that the static variables are not touched at all.
thanx once again
Neha
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
hey guys,
but if the constructor was Test (int,int) then using this.x=x++ changes can be made to static variables and the output will be as per the calculation.
Not like what Madan said that constructors do not return any values.This is true but not for static varibles.
Correct me if i am wrong

One more question
Can static variables and methods be accessed in the base class .
Do they need instance of its class to access it?
thanx in advance
Neha
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
A little mistake in my question
Can staic variable and method be accessed in subclass
Madan, Gopal
Ranch Hand

Joined: Aug 13, 2001
Posts: 86
Not like what Madan said that constructors do not return any values.

First, This is not what I said. I said, in your example, anything done to ARGUMENTS x and y inside methods(here constructors) are not reflected outside the methods(here constructors) because they are scoped out. Actually, they are being shadowed by static variables x and y, as explained in the previous posts.
Second, constructors DO NOT return anything
Now, if you change your constructor to Test(int x, int y) and use this.x and this.y on the LHS of the assignment statements you will get (6,10) as the output.
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Hey Madan
got it
thanx and sorry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: output