• 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

my confusion on method arguments

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone,
the foll question is taken from one of the mock exams.i fail to understand-
1)what is the effect of this(a)?
2)What value is passed into Test(int x) and why? if a is passed, then why? why not y?

class Test {
int y=1;
static int a=6;
Test() {
this(a);
}
Test(int x) {
if ( x > y )
a = y*2;
else
a = y*3;
a++;
}
public static void main(String [] args) {
Test t = new Test();
System.out.println(a);
}
}
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this(a) calls the constructor which accepts a single argument int.
U can work out the rest.
OUtput is 3..
-Anand
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test {
int y=1;
static int a=6;
// Test() passed into int a=6
Test() {
this(a);
}
//Test(a) sign Test(int x), x=a=6, x>y true
//if y=7 not 1, x>y false, output will be 22
Test(int x) {
if ( x > y )
a = y*2;
else
a = y*3;
a++;
}
public static void main(String [] args) {
Test t = new Test();
System.out.println(a);
}
}

>>Thanks Suneeta question, let me learn lot
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java we pass by value.
So copy of variable a is passed into the contructor and not the actual value.
And that should change the value of a in constructor and not the actual value of a.
But why the output is showing the permanent chang in the value of a??
Or is it the rule is different for Constructors than methods??
Anyone any comments.

Originally posted by michelle hou:
class Test {
int y=1;
static int a=6;
// Test() passed into int a=6
Test() {
this(a);
}
//Test(a) sign Test(int x), x=a=6, x>y true
//if y=7 not 1, x>y false, output will be 22
Test(int x) {
if ( x > y )
a = y*2;
else
a = y*3;
a++;
}
public static void main(String [] args) {
Test t = new Test();
System.out.println(a);
}
}

>>Thanks Suneeta question, let me learn lot


 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you see the code the value changed by making explicit assignments to a and not to x. If the code read

then there wouldn't have been any change to the value of a. Thanks for your poser. Had me foxed for a while.

Originally posted by Avinash Rai:
In Java we pass by value.
So copy of variable a is passed into the contructor and not the actual value.
And that should change the value of a in constructor and not the actual value of a.
But why the output is showing the permanent chang in the value of a??
Or is it the rule is different for Constructors than methods??
Anyone any comments.


 
suneeta prabhu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Avinash, Anshul,Michelle and Anand. Your replies made the picture clear as crystal.
reply
    Bookmark Topic Watch Topic
  • New Topic