Solved

What is equivalent way of overloading a variable?

Posted on 2000-05-17
6
146 Views
Last Modified: 2012-05-04
If I want to reference a variable in one of two ways by the same name as if it were a type 'var'.  How would I do this in Java? e.g.

String var1 = "";
Object var1;

In this case I want to use var1 to either contain a string used to make a connection, or contain a reference to an object that refers to an already open connection.
0
Comment
Question by:gary690774
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
Laminamia063099 earned 20 total points
ID: 2817821
You are making a reference to the C/C++ union construct.  In Java there is no equivalent construct.  There is a Java solution.

The best way is to make you variable of type Object.  Then, if it contains a string it will act as a string, and if it contains a connection, it will act as a connection.

If you need to check which it is at a certain time, use the instanceOf operator:

var1 instanceOf String

will return true if it's currently a string.  Then you can cast it to a string to call string specific methods:

String newString = ((String)var1).substring(1);

If you have any more questions, or anything here doesn't work as described, drop a note and I'll help you out.

Laminamia :)
0
 
LVL 16

Expert Comment

by:imladris
ID: 2817826
There is no way to overload a variable. Would it work to build an object that does what you want? For instance a connection management class with methods that return the state (already open or not), another method to return the string used, and another method to return a connection object. There could also, instead or in addition, be a method that always returns a connection object. It would, if needed, make the connection with the string you mentioned, and then return a connection object.

0
 

Author Comment

by:gary690774
ID: 2818066
Adjusted points from 10 to 20
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:gary690774
ID: 2818067
Laminamia,

   This seems to be a very reasonable approach.  I thought that type Object had some applicability here, but I wasn't sure of how to use it. This seems better than creating a 'Variant' class.

Thanks,
Gary
0
 

Author Comment

by:gary690774
ID: 2818072
Laminamia,

   This seems to be a very reasonable approach.  I thought that type Object had some applicability here, but I wasn't sure of how to use it. This seems better than creating a 'Variant' class.

Thanks,
Gary
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2818540
Yup, It's a good way to relate objects.  Any time that you can use a common parent for the variable, use it.  If you can ever use a non-Object parent (i.e. some other common parent other than object) it's better too.

Good luck!

Laminamia :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

864 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

21 Experts available now in Live!

Get 1:1 Help Now