• 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

Stack referencing stack

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i have written this program:

import java.util.Stack;
public class aClass
{
static final int i = 10;
public static void main(String []agrs)
{
Stack st1 = new Stack();
Stack st2 = new Stack();
aClass c = new aClass();
new aClass().Method(st1, st2);
System.out.println("st1 has: " + st1);
System.out.println("st2 has: " + st2);
System.out.println(i);
System.out.println(c.i);
}
private void Method(Stack st1,Stack st2)
{
System.out.println("st1 has: " + st1);
System.out.println("st2 has: " + st2);
st2.push(new Integer(100));

st1 = st2;
System.out.println("st1 has: " + st1);
System.out.println("st2 has: " + st2);
System.out.println(i);
}
}

But i am getting the o/p of str1 as 'blank'in the public static void main (String[] args) method.
Since i have assigned the str1 = str2 ,so i am supposing that str1 =100 also.
Please tell me the reason, why i am getting the value of str1 as blank.
Its very urgent .Please provide me the solution...if anybody can......
[ November 06, 2007: Message edited by: hamraj happy ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the output I get

 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith, I'm not sure how you got the "st1 has: [50]" line. When I ran it, I got the output I expected from looking at the code:



Hamraj,

Here's the reason why st1 is still empty after the Method() call ends: the reassignment of st1 in Method() does not affect the st1 in main(). When you pass a parameter to a Java method, you're passing only a copy of the parameter value or reference. Any changes you make to that copy within the method will not affect the original variable in the calling code. So in your program, the value of st1 in main() never changes after being initialization, i.e. it always refers to an empty Stack.

It's easier to recognize the problem if you use different names for Method's formal parameters:
Renaming a formal parameter makes absolutely no difference to the program if you change the name consistently within the method body. Thus the above program is completely equivalent to the one you posted. Hopefully this version makes the issue clearer.

By the way, it's a pretty common trick on mock exams (and probably also the real one) to give formal parameters the same name as the calling method's local variables. Don't fall for this! Just remember that if they do have the same name, you should regard it as nothing more than a coincidence--changing one variable does not directly affect the other.
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic