Changing an objects name and re-using original name?

Posted on 2000-05-17
Last Modified: 2010-04-01
I need to create an object using one standard name, but then change the name of the object and preserve that object and then re-use the original name again for a new object.

The objects will be instantiated through ActiveXObject automation server in an JScript/VBscript in ASP.  As such the class will have a Java COM dll wrapper.

Getting into Clones to do a 'deep-copy' seems a bit too-involved for this and I am hoping that a simpler 'switcharoo' (technical term) is available.

Question by:gary690774

Expert Comment

ID: 2819435
What's an object name?

Author Comment

ID: 2819499
An object name is any name you give it when instantiating a class.

  e.g. myclass objname = new myclass();
LVL 19

Accepted Solution

Jim Cakalic earned 0 total points
ID: 2819911
As you have described it, the object name is simply the name of a reference variable. A Java reference variable is a place to store a reference (or handle or pointer) to an object that has been allocated on the heap; all Java objects are allocated on the heap. When you say

    StringBuffer abc = new StringBuffer();

this is not substantially different in terms of the assignment being performed than saying

    int i = 5;

In both cases, you are assigning a value to some named holder in memory. It just happens that in the one case the holder contains 32-bit value and in the other it contains a reference (again, pointer or handle) to some piece of memory that has been allocated and initialized.

If I want to "reuse" the variable name, no problem. Here is how I would instantiate a StringBuffer, assign the reference to another StringBuffer variable, and "reuse" the first variable to hold a different StringBuffer:

    StringBuffer abc = new StringBuffer();
    StringBuffer xyz = abc;
    abc = new StringBuffer();

At this point in the program, you have created two StringBuffer objects. The variable xyz holds a reference to the object created by the first "new StringBuffer()" and the variable abc holds a reference to the object created by the second "new StringBuffer()". You can confirm for yourself that this is what is happening by printing the hashCode value at each stage of the construction and assignment. The hashCode value for each new StringBuffer object will be unique. I've broken it down a little further to show you how to do this:

    StringBuffer abc = null, xyz = null;
    System.out.println("1) abc=" + abc + ", xyz=" + xyz);
    abc = new StringBuffer();
    System.out.println("2) abc=" + abc.hashCode() + ", xyz=" + xyz);
    xyz = abc;
    System.out.println("3) abc=" + abc.hashCode() + ", xyz=" + xyz.hashCode());
    abc = new StringBuffer();
    System.out.println("4) abc=" + abc.hashCode() + ", xyz=" + xyz.hashCode());

Running this bit of code will yield results similar to:

1) abc=null, xyz=null
2) abc=3207485, xyz=null
3) abc=3207485, xyz=3207485
4) abc=3014669, xyz=3207485

As you see, at point 1, both abc and xyz are null. At point two, one StringBuffer object has been instantiated and assigned to abc. At point 3, both abc and xyz refer to the same StringBuffer object. And at point 4, abc has been assigned a reference to a second StringBuffer object while xyz retains its value referring to the first StringBuffer object.

Best regards,
Jim Cakalic

Expert Comment

ID: 2820777
Couldn't be said better, Jim.

Gary, that's your answer.   Each object variable is just a handle (i.e. pointer) to the object.  Set the second variable to point to the first object, then create a new object with the first one.

Good luck!

Laminamia :)
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.


Author Comment

ID: 2821981

   You guys are great!  I can't tell you how appreciative I am and what it means that you would take the time to go to so much detail to answer my question.  While I don't know how I can repay some of you guys, at least I can show my sincerest appreciation.
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2822047
Points are nice but I'll take eternal gratitude ;-)

Author Comment

ID: 2822064

   I haven't quite figured out this point thing.  It's unclear to me when to issue the points, because the entry box comes up in just about all screens.  Yesterday I ended up giving my self 20 points for reading a message.  I don't consider myself an idiot, but the way this thing is setup, it makes me feel like one.  Let me try to figure out how to reissue you some points.

LVL 19

Expert Comment

by:Jim Cakalic
ID: 2822354
Not a problem, Gary. And points really aren't necessary. I was joking.


Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Updating Java 9 87
nestparen challenge 4 57
Understanding websocket example in spring 1 43
Groovy:unable to resolve class error 2 32
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now