Solved

What is equivalent way of overloading a variable?

Posted on 2000-05-17
6
145 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java continue statement 10 72
Unable to open debugger port in Intellij idea 6 79
Java Timer (static) 9 10
Java DateChooser? 2 0
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

15 Experts available now in Live!

Get 1:1 Help Now