Solved

What is equivalent way of overloading a variable?

Posted on 2000-05-17
6
148 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
Technology Partners: 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

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!

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

685 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