• 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

Pass By Value

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Why "420" prints at Line 22 instead of "99" ?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answer is: because that's how Java works. It would be more practical if you explained why it should be 99, and then somebody could explain what your misunderstanding was.
 
Raja Narasimha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There should be some strong concrete reason because "Java" is created by Human beings. So, I believe that every question that we discuss in this Java forum has a very well valid reason. Hence, Please do not mislead others.
Coming back to the requested information, Because "num" variable is of primitive int type, any changes that you make at Line 11 should not be in scope once method at Line 14 is completed. Please let me know if my assumption is wrong.
 
Ranch Hand
Posts: 31
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Raja

In the code the main class is "P217_PassByValue01_Ask", which created an object "Test2 " which is referred with "test2Ref " at line 19.

In "Test2 " created an object of "PassByValueTest " with a reference "test1Ref " at line 06 in turn created a new object "PassByValueTest " with same reference "test1Ref " at line 11.

So in the program at line 22 "test2Ref.test1Ref.num" refers to the second object of "PassBy ValueTest" which has "num" value 420


Regard
varun
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 11 and 12- You are creating a new PassByValueTest and assigning a new value to the num.

In Java- Its always pass by value. So if you have Object references being passed around- We are actually copying the address/location of the Object. That way any changes made to the fields of the object gets reflected in other methods.
 
reply
    Bookmark Topic Watch Topic
  • New Topic