开发者

`*this = rhs` in Java?

I'm coming from the C++ world and I can't find what is the Java alternative (if any) to the following:

struct SomeStruct
{
    SomeStruct(){}
    SomeStruct(const SomeStruct& rhs)
    {
        *this = rhs;
    }
};

The reason why I need this is that I have a cache of existing objects, so I don't want to create another instance but just to 'clone' the existing one, something like this:

public class SomeObject
{
    private static Hashtable _objects;
    SomeObject()
    {
        SomeObject obj = _objects.get(some_key);
        if (obj 开发者_运维问答!= null) {
            // *this = obj;
            // instead of: 
            // this.something = obj.something;
            // this.something1 = obj.something1;
            // this.something2 = obj.something2;
            // a zillion fields....
        }
    }
};

EDIT:

Sorry, I confused some things (still need to learn both Java and C++).

Thank You


The closest is Object.clone() but read the relevant section from Effective Java first.

There are probably simpler ways to do what you want if you remove some of your requirements. For example, if you make your objects immutable then you don't need to copy the data into another object. Instead you can return a reference to the original object. This is much faster than a memberwise copy, and has lots of other benefits too, such as making it easier to write thread-safe code.


There's no direct equivalent. Object.clone() is the closest, but it creates a new instance and performs a shallow copy. You could write something generic using reflection though.


public class SomeObject
{
    private static Hashtable _objects;

    public static SomeObject getInstance(SomeKey some_key)
    {
        return _objects.get(some_key);
    }
}


It is unclear from your question whether you are trying to achieve a pure cache of immutable objects, or keep a cache of 'template' objects, and return copies that can then be mutated by the client. I am going to assume the latter.

Assuming that you do want to return copies of originals. there is no really nice way to implement a copy constructor in Java. Clone is marginally nicer, so you should hide the constructor behind a static factory method:

public static SomeObject getInstance(...) {
  SomeObject cached = ...;
  if (cached != null) {
    return cached.clone();
  }
  ...
}

Perhaps in your particular case you can separate the immutable and stateful parts of the objects? If so, some changes to your object model can lead to cleaner (and more efficient) code?


In Java, if you want to have a constructor corresponding to a copy constructor, you have to implement it yourself. In some cases, this means you have to copy fields from one object instance into another, while in other cases it means you have to implement a full deep copy - recursively iterate through the reference fields of the parameter in the copy ctor.

This depends on what you want - should the objects hashtable be copied as well? Or should both objects share the reference to it? For more info, see this question.


I don't know of any generic way to copy all the contents of fields from one object to another existing without using;

  • reflections
  • generated code.
  • copy using the Unsafe class.

Reflections is easier to use at runtime, but not as efficient as generated code. (But pretty close) I generate my data model so I use this option.

The Unsafe class is faster than reflection, but not cross platform and unsafe. It is almost as fast as generated code. ;)

In terms of speed, if generated code is 1x, Unsafe is 1.5 - 2x and reflection is 2 - 4x slower for highly cached data. For poorly cached data you won't see a difference.


You should use the Singleton pattern. You create a static instance of SomeObject and work with that instance.

   private static class SingletonHolder { 
     public static final SomeObject INSTANCE = new SomeObject();
   }

   public static SomeObject getInstance() {
     return SingletonHolder.INSTANCE;
   }

But don't fall in the Singleton trap, limit calls to getInstance()!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜