Solved

Changing an objects name and re-using original name?

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

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

813 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

11 Experts available now in Live!

Get 1:1 Help Now