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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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 :)

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

696 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