aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to get the answer as 15 for this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how to get the answer as 15 for this code" Watch "how to get the answer as 15 for this code" New topic
Author

how to get the answer as 15 for this code

rex tony
Ranch Hand

Joined: Aug 29, 2007
Posts: 159
Hello to all
Any one can you explain how to get the answer as 15 for this code
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Could you please explain, where is your doubt in the code?


Thanks and Regards
rex tony
Ranch Hand

Joined: Aug 29, 2007
Posts: 159
Hello Maheshwari,
I couldn't understand the flow of the program.Can you explain it now?
regards
rex
rex tony
Ranch Hand

Joined: Aug 29, 2007
Posts: 159
Hello
can you explain this stage
[code]
int add(AClass ref){ return x + ref.x; }//end add()
[/code/
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
It is not clear from your statement "how to get the answer as 15 for this code", what exactly are you looking for.
Your code already gives the answer as 15 on running it. Why at all do you need to do any changes in that for getting answer as 15?

The flow of current code is as follows:
1. An object of type AClass is created in heap and is assigned to reference 'ref1'. This object will have its internal state as 5.
2. Another object of type AClass is created and is assigned to reference 'ref2'. This object will have its internal state as 10.
3. A copy of 'ref2' will be created and will be given the name 'ref'. Thus AClass object with state 10 shall have two references viz., 'ref2' and 'ref'.
4. The add method of AClass object with state 5 will be called by passing it the copied reference 'ref'.
5. The add method will add the AClass object (with state 5) x value with that of AClass object (with state 10) x value and return it.
6. This value (15) shall be printed on console.

I hope this makes sense.
Mack Stevens
Greenhorn

Joined: Aug 31, 2007
Posts: 17
Declare variable x as public(or anything other than private) in class AClass
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy "Mack Sr" !

Thanks for your first contribution to this forum and...

Welcome to the Ranch!




Hope you'll enjoy.




Only one small issue: The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp


So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.


I'm posting this because I am one of the moderators of this forum.


Yours,
Bu.


all events occur in real time
Mohammad Hossain
Greenhorn

Joined: Jul 26, 2007
Posts: 14
How would you get access to the private member x. You need a public method, or you need to make the variable public.


Live Free Or Die
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by Mohammad Hossain:
How would you get access to the private member x. You need a public method, or you need to make the variable public.


Private does not mean private to the instance. It is private to the class. This means that an instance can actually access the private member of another instance, provided that both instances are of the same class type.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mohammad Hossain
Greenhorn

Joined: Jul 26, 2007
Posts: 14
Thanks I have got it now
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to get the answer as 15 for this code