Skip to content
View in the app

A better way to browse. Learn more.

Tip.It Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Java - clone() method

Featured Replies

Hey there,

 

Well - getting forward with my game, but I'm finding I'm in need of 'cloning' objects, as such; reusing all the data from one object in another one, in a similar fashion to the 'ByVal' implementations in other languages.

 

However, I've never looked into this in Java before. With a bit of searching around, it seems that implementing your own clone method is the most viable solution, but I'm wondering if anyone else has more experience in this area and some potential solutions. :)

I would say that using your own clone() method is probably a good idea. Just make so clone() makes so that that the correct properties are made unique or reset so that you can differentiate between objects.

C2b6gs7.png

Hmm, I'm not sure exactly what you're trying to do (never used byVal) but couldn't you just do something like this?

 

Object a (property a, property b, property c); //object you want to clone

 

to clone:

 

Object b(a.getProperty(a), a.getProperty(b), a.getProperty(c));

 

Unless I'm not understanding what you want to do that seems like an easy way to do it.... If you wanted a unique object ID or something you could do

 

a(1, b, c);

 

b(1+a.getProperty(id);

polvCwJ.gif
"It's not a rest for me, it's a rest for the weights." - Dom Mazzetti

That seems like a pretty roundabout way of doing it IMO... clone methods are easy to write and make for more readable code.

C2b6gs7.png

That seems like a pretty roundabout way of doing it IMO... clone methods are easy to write and make for more readable code.

Wouldn't a clone method basically just contain the same thing?

polvCwJ.gif
"It's not a rest for me, it's a rest for the weights." - Dom Mazzetti

Yes, but it enables you to write the code that resets the properties only once- and invoke it in one line of code. Also, nesting functions isn't nearly as readable as just one function call.

C2b6gs7.png

Quite true dear sir.

 

I shall now attempt to write such a method being extremely bored at work..

 

EDIT: Never mind, I won't, I forgot I don't have Java :(

polvCwJ.gif
"It's not a rest for me, it's a rest for the weights." - Dom Mazzetti

  • Author

Thanks for the replies! :) I did indeed end up just writing my own clone methods, they're surprisingly easy with most classes actually, certainly more so than I had imagined.

 

I was rather surprised that there was no 'built-in' way to easily get another 'clone' of an object in java without writing your own method actually, but then I suppose that it's rarely needed, so writing a few methods yourself is hardly any trouble.

 

If interested, currently the area that it's being utilised in is within levels of my game. Once all the levels have been completed, large modifications have been made to the objects that the level is comprised of so resetting the level involves changing all the objects back to their original state. Doing so via cloning is a relatively easy solution. Whenever a new level is created, a clone is placed in an ArrayList which contains all levels in their 'initial' state, allowing me to replace a modified level with a clone of their initial-state counterpart whenever it should be played again.

 

Another option was to define a structure which would contain all the objects at their initial state of the level and create a constructor for a level based on the structure - again, would involve cloning too.

  • 1 month later...

Don't forget to implement the Cloneable interface for classes that have a clone() method.

  • Never trust anyone. You are always alone, and betrayal is inevitable.
  • Nothing is safe from the jaws of the decompiler.

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.