Solved

What is equivalent way of overloading a variable?

Posted on 2000-05-17
6
147 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 19
xampp tool 12 48
how to add new optional parameter to JSP 1 40
jdbc error in jsp application 20 44
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

808 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