• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

output

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A little mistake in my question
Can staic variable and method be accessed in subclass
 
Madan, Gopal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Madan
got it
thanx and sorry
reply
    Bookmark Topic Watch Topic
  • New Topic