Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What is equivalent way of overloading a variable?

Posted on 2000-05-17
6
Medium Priority
?
151 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
[X]
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
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
Laminamia063099 earned 80 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
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!

 

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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

705 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