• 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

class instance variables modifying

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Take a look at the modify method...



Is it actually modifying anything important?

Henry
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tc inside modify is local and points to new TestClass object.
The other tc (instance field that you try to access in main) still points to the earlier TestClas object.

TestClas object created in the modify method is not accessible outside the modify method (as it is referred by tc which is local var)

Hope this helps.

_charles
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are passing the reference of the object you have created in main method but because you have modified the reference of the local variable tc in the method modify so it will point to different object. But the actual object that is in main method is still pointing to the older object created in main method so the changes you have made in the modify method will be limited to the local variable tc only.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup. That's related to Pass by Reference!
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I think what Harpreet was describing was actually Pass by Value. The address value of the actual object was passed into the modify() method. Internally in the modify() method, the tc variable initially has that address value but then was given another address when the method instantiated a new TestClas object for the tc variable to point at.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Larry Chung wrote:No, I think what Harpreet was describing was actually Pass by Value. The address value of the actual object was passed into the modify() method. Internally in the modify() method, the tc variable initially has that address value but then was given another address when the method instantiated a new TestClas object for the tc variable to point at.



Hi, Larry Chung
have a look at this link

if we make a alternation to the reference variable, which we pass to the method, modify() rather than instantiating new one, It'll affect the object referenced by the reference variable tc(in that main method reference variable).

it's true that object can't pass, only the reference variable value(bit pattern).... But we called it as Pass by reference.
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, Java is pass-by-value according to that link
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Larry Chung wrote:No, Java is pass-by-value according to that link



Ok. Then what is the difference between Pass by value and Pass by reference. According to you, All are Pass by Value? There is no concept called as Pass By reference correct?

I mean, that object reference variable are passed by reference, We can't pass a object. It's correct that the bit pattern of the reference variable's value is passed. But the mechanism is called as Pass by Reference. Somebody confirmed???
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

Ok. Then what is the difference between Pass by value and Pass by reference. According to you, All are Pass by Value? There is no concept called as Pass By reference correct?



That's an easy question.
In Java, there is only pass-by-value. There is no such thing as "pass-by-reference" in Java.
 
I child proofed my house but they still get in. Distract them with 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