Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Changing an objects name and re-using original name?

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
gary690774
Asked:
gary690774
1 Solution
 
falterCommented:
What's an object name?
0
 
gary690774Author Commented:
An object name is any name you give it when instantiating a class.

  e.g. myclass objname = new myclass();
0
 
Jim CakalicSenior Developer/ArchitectCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Laminamia063099Commented:
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
 
gary690774Author Commented:
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
 
Jim CakalicSenior Developer/ArchitectCommented:
Points are nice but I'll take eternal gratitude ;-)
0
 
gary690774Author Commented:
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
 
Jim CakalicSenior Developer/ArchitectCommented:
Not a problem, Gary. And points really aren't necessary. I was joking.

Jim
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now