Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get the answer as 15 for this code

 
rex tony
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to all
Any one can you explain how to get the answer as 15 for this code
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please explain, where is your doubt in the code?
 
rex tony
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Maheshwari,
I couldn't understand the flow of the program.Can you explain it now?
regards
rex
 
rex tony
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
can you explain this stage
[code]
int add(AClass ref){ return x + ref.x; }//end add()
[/code/
 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare variable x as public(or anything other than private) in class AClass
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohammad Hossain
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you get access to the private member x. You need a public method, or you need to make the variable public.
 
Henry Wong
author
Marshal
Pie
Posts: 20999
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mohammad Hossain
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I have got it now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic