File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class instance variables modifying

 
Heba Mahmoud
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Henry Wong
author
Marshal
Pie
Posts: 20830
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Take a look at the modify method...



Is it actually modifying anything important?

Henry
 
Charles Chikito
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup. That's related to Pass by Reference!
 
Larry Chung
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • 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
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, Java is pass-by-value according to that link
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic