File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes class instance variables modifying Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "class instance variables modifying" Watch "class instance variables modifying" New topic
Author

class instance variables modifying

Heba Mahmoud
Ranch Hand

Joined: Apr 26, 2009
Posts: 62


SCJP Pass 85%....
Thousands of candles can be lighted from a single candle, and the life of the candle will not be shortened
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18752
    
  40


Take a look at the modify method...



Is it actually modifying anything important?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76

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

Joined: Jan 14, 2010
Posts: 317

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

Joined: Nov 04, 2009
Posts: 2066

yup. That's related to Pass by Reference!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
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.


SCJP 6
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Feb 02, 2010
Posts: 247
No, Java is pass-by-value according to that link
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Feb 02, 2010
Posts: 247
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: class instance variables modifying