Solved

Changing an objects name and re-using original name?

Posted on 2000-05-17
8
197 Views
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.

Thanks,
Gary
0
Comment
Question by:gary690774
8 Comments
 
LVL 3

Expert Comment

by:falter
ID: 2819435
What's an object name?
0
 

Author Comment

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

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

Accepted Solution

by:
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
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:Laminamia063099
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 :)
0
 

Author Comment

by:gary690774
ID: 2821981
Jim,

   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.
0
 
LVL 19

Expert Comment

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

Author Comment

by:gary690774
ID: 2822064
Jim,

   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.

Gary
0
 
LVL 19

Expert Comment

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

Jim
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 50
oracle 11g 23 106
Android development question 2 51
jmss example java 2 23
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

791 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