• 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

q from marcus green

Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class ValHold{
public int i = 10;}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod(); }
public void amethod(){
int i = 99;
ValHold v = new ValHold();
System.out.print( v.i ); }//End of amethod

public void another(ValHold v, int i){
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.print(i); }//End of another}
1) 10030
2) 20030
3) 209930
4) 10020

i dont understand how to solve this problem?
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Starting from main method:

object of obparm is created and reference is stored in o.
amethod with reference of o is called.

Execution os amethod:
i = 99.
Creates object of ValHold and saves object reference in v.
Sets value of i in v to 30.
calls another method passes parameters as 1. v created as above with value 30 and 2. i (= 90).

Execution os another:
Gets parameters in local variable v and i respectively.
set value of local variable i to 0.
sets value of i in object v to 20.
creates new object of VahHold and assigns it to variable vh.
local variable v is reassigned to new reference of vh.
System.out.print(v.i) will print reassigned value of local variable that is - 10
System.out.print(i) will print local value of i that is 0
System.out.print( v.i ) will print actual value changed by statement "v.i = 20" = 20.
so, it is 10 0 20
What's wrong? Where are you going? Stop! Read this tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic