Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8108003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:57:29+00:00 2026-06-06T00:57:29+00:00

I am coding a text based Java game, I have ran into a few

  • 0

I am coding a text based Java game, I have ran into a few issues I cannot get my head around.

My code works along these lines:

        public class main() {
           run initial method for menu
           Player player = new Player(name,1,1,1);
           do{
           game code
           while(running = true)
          }

       public class Player() {
           string name
            int age
            int level
            int exp

           getName()
           setName() etc etc
          }

       public class Train() {
        kill monster
        totalExp = monsters killed

        }

Now, the problem is, how do I pass the exp gained to my player class which has my get and set methods? The exp is calculated/generated in the Train class, but I need to pass it through to Player so I can use .set/.get and display the updated information in my Main class.

Would adding:

Player player = new Player(name,1,1,1) into the Train class just create a NEW object of Player so I would have two, and assign the exp to the player in Train() but leave the one in Main() alone.

Many thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:57:30+00:00Added an answer on June 6, 2026 at 12:57 am

    You are correct in your assumption that adding a new object of Player in train would leave another unaffected. What you can do is add a reference to a Player in your Train class, and assign it in your constructor. E.g.

    class Train {
        Player player;
        public Train(Player p) {
            player = p;
            /* ... */
        }
        /* ... */
    }
    

    You can then call player.method() in the train class, and it will update the Player you passed to the constructor.

    Thus, when you create an instance of Train, pass it the Player you have already created and it will update the player based on what happens in Train.

    Player p = new Player(...);
    Train t = new Train(p);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the following coding which edits my text file based on the value
I am currently making this text-based rpg game with a simple GUI. I was
Alright let me explain myself here: I am making an online text based game.
Background: Zen coding is apparently the name of a style of text-entry in an
I've used xml type by writing (asp coding) Response.ContentType = text/xml Now xml contents
I know to set Typeface from coding like below. type1=Typeface.createFromAsset(getAssets(),fonts/helveticaneueLtstd-bd.otf); text.setTpeface(type1); But it is
I have to render some text to a web page. The text is coming
I have NSString like My name is. I want to remove text after coming
I have text I am displaying in SIlverlight that is coming from a CMS
I have this kinda template text : Hello {#Name#}, Thanks for coming blah on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.