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

  • SEARCH
  • Home
  • 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 7530621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:57:40+00:00 2026-05-30T04:57:40+00:00

I was working on a game recently in C++ where I implemented the Command

  • 0

I was working on a game recently in C++ where I implemented the Command Pattern to manage keyboard input to control a spaceship. When instantiating all the commands, I’d pass the spaceship pointer to each constructor so all commands were working with the same spaceship object.

This pattern made sense in C++ because you can pass by reference, but in Java everything is pass-by-value. If I tried to implement the same thing in Java, how would I have each command pointing to the same object?

In all the examples I’ve seen of the Command Pattern used in Java it makes no difference whether the member variables of each Command are copies or references.

  • 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-05-30T04:57:41+00:00Added an answer on May 30, 2026 at 4:57 am

    Java does pass by value in all cases, however all ‘non-primitive’ types exist as references, so for types like String, List or Spaceship you’re actually passing a reference around – the pass by value is because you’re passing the reference by value. Which is a pretty confusing way of expressing it, I always felt.

    Anyway, the upshot of it is that if you have a (hugely simplified) Command class

    public class Command {
        private Spaceship spaceship;
        public Command(Spaceship ship) {
            spaceship = ship;
        }
    }
    

    And then you do this:

    Spaceship ship = new Spaceship();
    List<Command> ships = new List<Command>();
    for (int i = 0; i < 40; i++) {
        ships.add(new Command(ship));
    }
    

    then every single one of those Command objects has a reference to the same Spaceship object. Passing the reference by value does not cause a copy of the Spaceship object that the reference points to. However, if you were passing ints around, you would indeed be passing copies.

    Just remember the distinction between primitive types like int, and object types like String and you’ll be fine. It’s helpful to remember that primitive type names begin with a lowercase letter (and can never be user-defined), while object types begin with a capital letter. All user-defined types are object types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I run a browser based game at www.darknovagames.com. Recently, I've been working on reformatting
I've been working on a 2d game in java recently, and am currently trying
I'm currently working on a game/engine that uses OpenGL for rendering, and recently have
I'm currently working on an OpenGL game, and recently began refactoring it to support
I'm working on a Monopoly based game with properties, money, cards, etc. I recently
So, recently, I've been working on a JS/HTML5 Game Engine. Right now I'm calling
Recently I have been working on a multiplayer networking app. I had successfully implemented
A game Im working on has several XML files it uses to manage sprite
I'm working on an android game, and just recently made the step to SurfaceView
I'm working an on iOS game and recently tested on an iPhone 4S. Siri

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.