File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Calling a method of an object from another object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling a method of an object from another object" Watch "Calling a method of an object from another object" New topic
Author

Calling a method of an object from another object

Hans Hovan
Greenhorn

Joined: Mar 03, 2013
Posts: 28
(Hopefully my terminology is correct)

I am calling a method of an object from another object.

So for example I have this in main()


The encounter class has this:


The player character class has this:


So, to my understanding in main I am creating an Encounter object, then calling the Encounter object that is calling the PlayerCharacter object within it to do something (get a name in this case).

This seems to work when I've tested it.

My question is if there is a way to call a method of an object from another object that is a different class from where the object originally came. That was a mouth full..
In other words, if I have another class:


If I make an Encounter object in the Shop class though, a new PlayerCharacter object will be created within the Encounter object, but that PlayerCharacter object is a new one so I won't be able to alter the instance variables of original PlayerCharacter object...which is what I want to do. Of course I can have the methods of 'Shop' in the Encounter class and that will work. But for the sake of breaking up code into more readable/manageable chunks is there any way to do this?

Hopefully that question makes sense.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Hans Hovan wrote:If I make an Encounter object in the Shop class though, a new PlayerCharacter object will be created within the Encounter object, but that PlayerCharacter object is a new one so I won't be able to alter the instance variables of original PlayerCharacter object...which is what I want to do. Of course I can have the methods of 'Shop' in the Encounter class and that will work. But for the sake of breaking up code into more readable/manageable chunks is there any way to do this?

Hopefully that question makes sense.

Sort of...I think .

What about:That way, you can't set up a Shop without supplying it with an Encounter instance. Then your main() code might look something like:
PS: What you're proposing is not very safe.
  • For one thing: you shouldn't give clients direct access to your fields.
  • Secondly: You certainly shouldn't allow clients to update your fields directly. It's a recipe for disaster.

  • HIH

    Winston

    Isn't it funny how there's always time and money enough to do it WRONG?
    Artlicles by Winston can be found here
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Calling a method of an object from another object
     
    Similar Threads
    what is the result and why?please help me!
    Head First Guessing Game Question
    IDE or bust?
    Simple wait notify
    Calling a variable form the main class