• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to access/use the the same instance of an object in multiple classes?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm learning Java and decided to make a little RPG/Dungeon exploring game as an exercise.

I'm using multiple classes and trying to learn the OOP way.

I have a Character class which has all the instance variables/methods for my Character (this is just an example I made up)



So, let's say I make a character in my Main method



Now I can do things like lower the HP if he gets whacked by a goblin (or whatever bad guy).

But let's say I have a different Class that holds all of the possible encounters, or a Class that controls a combat interaction, etc.



I'm not sure what the best way to go about this would be? I was thinking I'd need to construct a new Character object everytime I needed to access the character in another class and just pass in the arguments of whatever the current state of the character is and then somehow send them back or? I'm really a bit stumped as to what the best most efficient way to do this would be.
 
Ranch Hand
Posts: 51
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thing I have to ask is does this program have user interaction? Or is everything going to be done in the code and we just see a finished product?
And also how experienced are you in java, do you have a solid understanding of methods, objects, and classes?
But I still don't quite see what you're trying to accomplish here. Maybe someone else can be of more help.
 
Hans Hovan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, guess I should have included more backstory.

I'm pretty new in Java...basically have taken a Java 101 class and practiced some on my own.

I have a decent understanding of objects, classes, methods etc. But not advanced ways to implement them.

The program DOES have user interaction. Basically the user can move around on a grid (up/down/left/right) and when the user encounters a space they do things like press 1 to attack, 2 to use a spell, blah blah.

So, I have my character object which stores the character's HP, experience, items, etc. But I need to be able to access that character object from multiple different classes. For example I have a class that deals with encounters and the character has to be a part of that and a class that handles combat and the character has to be a part of that as well as being an object in the main method. I might just not be using terms correctly?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object in the main method? You should move things out of the main method. I presume your game class will have a field which collects characters; you should be able to find the characters from that.
 
Hans Hovan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so if I have my character object in my Game class, would I have to have all my methods for combat (for example) in there?

I guess what I'm trying to ask is if I create my character object in the Game class I use that character in the Game class to set and get attributes of my character in the Character class. But how can I do anything with that character in any other classes besides my Game class?

I suppose this is more of a design question. I could figure out how to make all of this work in one giant class but I'm trying to figure out how to better compartmentalize. I'll try writing a little bit of code and then try and ask a more specific question.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic