#What is the correct way to obtain/pass a copy (not a reference) of an object?

37 messages · Page 1 of 1 (latest)

livid oxide
#

I asked ChatGPT this and it gave me three options:

2. Copy() constructor
3. Deep copy (using this.value)```
    
My original object does not have a cloneable interface or a copy() constructor, and it doesn't seem appropriate to include one because I am only making a copy for one specific command out of multiple commands.

Essentially what I am trying to do is to change one attribute of a `Skill` object (which contains multiple other attributes) but I do not want this to affect the original Skill object. 

Illustrated example: Maybe I want to change the duration from 40000ms to 600000ms for just this invocation of a `SpecialBuff()` command. However outside of invocation in this way, I want the duration to remain at 40000ms.

My first thought was to instantiate a copy and then run through the usual method with a copy of the original object, which led to this question.
gentle zenithBOT
#

This post has been reserved for your question.

Hey @livid oxide! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

toxic tapir
#

you create the same object with the same field

#

it will be a copy of it

livid oxide
#

what do you mean

#

the Skill object has like 80+ attributes

#

idk if all of them have accessors/mutators

toxic tapir
#

you just create an skill Object with the same constructor that you use for instantiating the last skill object

#

for Exampl Skill skill = new Skill(parameters) Skill skillCopy = new Skill(skill.getparam)```

livid oxide
#

I don't think you understand

#

the skill objects aren't initialized manually

#

they are loaded from .xml

#

idk if there is even a constructor

#

there is a method that loads like 500 skills

#

and maps them to kvp based on id

#

I just want to grab a copy of a specific skill and modify the duration after it's been loaded in

livid oxide
toxic tapir
#

no I just Imagined that the skill Object had getter Methods to acesss it's field

livid oxide
#

What's the danger of doing this? 🤔

            int temp =  skill.getDuration();
            skill.setDuration(999000);
            skill.applyTo(player);
            skill.setDuration(temp);
        }```

Could it lead to trouble if multiple players execute this same command in unison?
charred mason
#

It's unworkable in multithread and extremely fragile in other circumstances. You're always at risk that an exception/error happens at any point, such as for example before you restore the original duration.
Which will leave your Skill object in a state that will be at best confusing for the rest of the execution until the program finishes crashing

#

There is no direct answer on how to make copies with one change.
You could look into how LocalDateTime does it

#

Having 80 actual fields, though, is essentially a failure. Do differently. That's often how good programming go back to sound sane

#

Personally I think adding cloneability would be the better approach

livid oxide
charred mason
#

It creates a new LocalDateTime like the one you already have, except the hour of the day is different

sweet sedge
#

Essentially, turn your class into a record to make it immutable, so any operation returns a new instance. Immutability is generally a good idea when doing a lot of work with larger data

gentle zenithBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

sweet sedge
#

Just look up "java records" and see about converting to that, and see if it's worth it

livid oxide
sweet sedge
#

Idk where you got that claim, they are about just as easy to serialize as normal classes

livid oxide
#

No he said it is more reliable to serialize than regular classes

#

Not less

woven moon
#

seems like there might actually be some difference between serialization of records and normal classes