1

I have a method called xyz which returns an object.

private XYZ xyz() {
     return abc;
}

I want to use this method in 2 different places but I want to call the method only once.

I mean for the first time I am calling the method to get the object.

   XYZ aaa = xyz();

Here I have an object 'aaa'. And I want to use the same object in different place, but I can't use this object as this is private. So what I want to do is to create a new object called 'bbb' from 'aaa' without calling the method. Just like bbb = aaa; Please help me how to create a new object from existing object or create a duplicate object with different name.

Thanks in advance

ran
  • 667
  • 4
  • 9
  • 16
  • 1
    what you need is cloning, you should read how to clone objects in Java, for example here: http://www.go4expert.com/forums/showthread.php?t=5424 – Davide Piras Nov 18 '11 at 22:15
  • `duplicate object with different name` - if you mean how to have two names for the same variable *(like `&` in C++ or `ref` in C#)*, I don't believe that can be done in Java. If you mean have two variables point to the same reference, you just do `bbb = aaa`, exactly as you wrote. Or are you asking how to write a getter? Or maybe how to clone an object, so you have two separate objects which are otherwise equivalent (Java has the `clone()` method for that, which you may have to override). – BlueRaja - Danny Pflughoeft Nov 18 '11 at 22:16
  • 1
    Do you want a different object with the same field values as the first, or do you actually want to create another reference to the same object? – Shaun Nov 18 '11 at 22:17

5 Answers5

2

You could use the clone method by implementing the cloneable inteface: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Cloneable.html

AlexTheo
  • 4,004
  • 1
  • 21
  • 35
1

So you want only one instance - you can call the xyz() method twice, it won't create a new object.

And if you want two instances - you need cloning. Check this answer.

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
1

I think he wants to clone XYZ object because it is private in his code (and so is the getter method) and not useable outside. One other idea is to make the getter public or write a public getInstance() method (for example) returning the Singleton.

http://en.wikipedia.org/wiki/Singleton_pattern

Mechkov
  • 4,294
  • 1
  • 17
  • 25
  • i think i need to use this clone concept. if i use b = a.clone() is this correct....? i want to create object 'b' same like object 'a'. can i use this syntax......???? – ran Nov 18 '11 at 22:30
  • I was thinking because you couldn't use the private Object, that's why you wanted to clone it. Fair enough. – Mechkov Nov 18 '11 at 22:31
  • u right mechkov. i have a private object so i can't use that object in other place. so what i want to do is i want to create a new public object by cloning the existing object. so that i can use this object whereever i want. this is my idea. – ran Nov 18 '11 at 22:33
  • 1
    @Cool. You could try this: XYZ z = (XYZ)aaa.clone(); Dont forget to cast because clone() returns an Object. – Mechkov Nov 18 '11 at 22:34
  • thank u so much. so here 'z' and 'aaa' both are objects of XYZ right...? if this is correct i exactly need this only. plese confirm this. – ran Nov 18 '11 at 22:36
  • If you arent sure you can test to see if "z" is-A of type XYZ. use instanceof operator. If(z instanceof XYZ) return true; – Mechkov Nov 18 '11 at 22:38
  • sorry guys, since i am new to java i could'nt explain my question properly. but really thanks for your reply's and answers. – ran Nov 18 '11 at 22:51
  • hi mechkov. still i am stucked with cloning the object. to clone the object do i need to implement cloneable interface. because here my class is a jar file(i mean API). so i can't edit the class. but in my eclipse i am not getting any option to clone the object. is there any other way to clone the object without implementing the cloneable interface. please explain. – ran Nov 19 '11 at 05:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5146/discussion-between-ran-and-mechkov) – ran Nov 19 '11 at 06:50
0

What you are talking about can be done using a copy constructor:

example:

private field1;
private field2;

public XYZ(XYZ in)
{
   this.field1 = in.field1;
   this.field2 = in.field2;
}

So this constructor takes as a parameter an object of type XYZ called in and copies all of in's fields to this XYZ object.

I think it would be better if you just had a method called getXYZ() that returns a reference to an XYZ object.

public XYZ getXYZ()
{
   return this;
}
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
0

maybe you could try this out: (for deep cloing)

http://code.google.com/p/cloning/

Kent
  • 189,393
  • 32
  • 233
  • 301
  • Hi All, thanks for your reply. actually i want 2 different object of the same method but i want to call the method only once. – ran Nov 18 '11 at 22:27
  • i think cloning is the only better way to create a copy object....my issue was fixed by implementing cloneable interface.. – ran Sep 25 '12 at 21:01