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 6075209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:28:08+00:00 2026-05-23T10:28:08+00:00

class Player { private Location location; public Location getLocation() { return location; } public

  • 0
class Player
{
   private Location location;

   public Location getLocation()
   {
        return location;
   }

    public void setLocation(Location location)
    {
        this.location = location;
    }
}

…

class Location
{
    int x,y,z;

    public Location(int x, int y, int z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public Location(Location location)
    {
        this.x = location.x;
        this.y = location.y;
        this.z = location.z;
    }


    public void updateLocation(Location location) //the fix..
    {
        this.x = location.x;
        this.y = location.y;
        this.z = location.z;
    }
}

Say.. you do

Player p1 = new Player();
Player p2 = new Player();
p1.setLocation(p2.getLocation());

Now the bug/problem occurs when you attempt to modify other persons location. Both players locations changes exactly the same, as they both now share the same location.

So of course this below will work just fine.

p1.setLocation(new Location(p2.getLocation()));

but the problem is it always creates a new object.. when I could just update the existing instance..? how do I update the existing instance on default without making my own methods like I did below to fix this.

I had to fix this using the method below (any way to do this by default without doing like this below)

    public void setLocation(Location location)
    {
        if (this.location == null)
            this.location= new Location(location);
        else
            this.location.updateLocation(location);
    }

Anyone knows any tricks I may not know about? Thanks.

  • 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-23T10:28:08+00:00Added an answer on May 23, 2026 at 10:28 am

    Classes are reference types. When you call this:

    p1.setLocation(p2.getLocation());
    

    you are not passing instance data to the method but the reference to the instance. Reference is like managed pointer to heap (memory) where the instance’s data are stored. That is quite important because otherwise all changes to instance’s data would have only method scope. When you call this:

    this.location = location;
    

    you are assigning the reference not the data. It means that both location and this.location will point to the same block in the memory.

    That is the essential point in C# programming. If you want data to be copied instead of referenced you must use structures instead of classes.

    Anyway reassiging values from one instance to another instance is quite common task.

    Edit:

    You pointed that you don’t want to create new object because of overhead.

    • Premature optimization is source of evil
    • Optimize where it make sense and where you see that it really make performance drop. Here you have some values about object instantiation on simple laptops.

    Also saying that you don’t like properties is strange – you are programming in C# so use it correctly otherwise your colleagues will not like you.

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

Sidebar

Related Questions

#include player.h class team { public: team(); void addPlayer(player); void deletePlayer(int); double getTotalCost(); int
public class VideoDemo extends Activity { private VideoView video; private MediaController ctlr; File clip=new
I have a class containing two properties: public class player{ public player(String playerName,int points){
My model: class Player(models.Model): player_name = models.CharField(max_length=50) player_email = models.CharField(max_length=50) def __unicode__(self): return self.player_name
I have this Player class which implements the Comparable interface. Then I have an
Given the following code : public abstract class Participant { private String fullName; public
I have this in my first activity: private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener() {
#pragma once #include Player.h class Player; //class SmallHealth; const int kNumOfCards = 3; //for
I have a class Player that contains a list of Accessory objects. There are
i have the following models setup class Player(models.Model): #slug = models.slugField(max_length=200) Player_Name = models.CharField(max_length=100)

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.