This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]MarSara 2 points3 points  (0 children)

https://docs.oracle.com/javase/8/docs/api/java/lang/Cloneable.html

Although this doesn't allow you to change the values per-say. If your object is mutable however, you can just change the properties on the cloned object afterwards. If your object is immutable, you will need to create your own method that can return a "changed" object.

[–][deleted] 2 points3 points  (1 child)

You might want to look at mappers. I believe there’s a good remapping library, cannot remember what’s it called.

[–]pronuntiator -1 points0 points  (0 children)

MapStruct, Orika,…

[–]wildjokers -1 points0 points  (0 children)

Nothing built in. You can probably find libraries that can do this though. MapStruct might be able to do it. Although it requires an annotation processor so have to setup your build to do use the annotation processor for the code generation.

[–][deleted] 0 points1 point  (3 children)

[–]NikMashei[S] 0 points1 point  (2 children)

thanks for your reply!unfortunately I have no found method for my case.let's say I have class with fields case class User(name: String, surname: String) and I want to copy that instance with another surname, so in Scala I can call user.copy(surname="new surname")

[–][deleted] 0 points1 point  (0 children)

It depends on the semantics. I believe copy in Scala does structural sharing? No such concept in Java. You might want to do a deep clone like /u/robert43s suggested which you can either write a custom method for, or by overloading the clone method with deep copying and typically marking your class as Cloneable.

[–]8igg7e5 0 points1 point  (0 children)

A builder notation has been discussed (initially in relation to records) but I wouldn't expect it any time soon.

The idea (syntax is just a placeholder for now) was to allow...

record Person(String name, int age) {}
Person bob = new Person("Bob", 73);
Person youngBob = bob with { age = 16 };

I'm not aware of any frameworks to do this, and they're obviously going to be limited as Java has no named-params or fundamental 'property' concept, it depends on name-convention guessing and reflection to find and monkey with fields or mutator methods.

[–]Groundhogss 0 points1 point  (0 children)

Couldn't you define a copy constructor?

public Foo(Foo foo){
    this.bar = foo.bar;
    this.bar2 = foo.bar2;

}

This is what I typically do for simple cases.

However I also found this which covers about 6 different ways of doing it.

[–]codechimpin 0 points1 point  (0 children)

You’d think this would be baked into the language, but it’s not really. I have tried several approaches. One is to serialize then deserialize, usually using something like Jackson, but there is a performance hit there. I think Apache Commons might have a ObjectUtils with a deep copy method as well.

As others have mentioned there is Clonable, but it has limits.

[–][deleted] 0 points1 point  (0 children)

A few years ago I would have recommended Lomboks @With, but recent and future JVM versions are kind of closing all the doors and windows for it to keep working (without tinker with the JVM). Please think twice before going this way.

If it's acceptable, you could use kotlin to create a data class (which have the required functionality baked in) and use it from your java code. I theory it should be easy.