wood burning stoves 2.0*
The moose likes Java in General and the fly likes How could I declare variable of a type determined after the program's running? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How could I declare variable of a type determined after the program Watch "How could I declare variable of a type determined after the program New topic
Author

How could I declare variable of a type determined after the program's running?

Tom Josephits
Greenhorn

Joined: Mar 11, 2012
Posts: 28
I have a program and I need it to be incredibly modular. It needs to be able to store data of data type that will be determined by an XML after the program's running. How could I do this?
One idea I had was to make an array of objects and save them in that, but I want to know if there's a better way?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

1. Declare it as Object.

2. Have all the types you'll be using implement a common interface, and declare it as that type.

3. Have different code branches using if or switch or the Command Pattern that have Java code declaring it to be of the appropriate type.

It's hard to be any more detailed without more specifics about your requirements beyond, "I want it to be flexible."
Tom Josephits
Greenhorn

Joined: Mar 11, 2012
Posts: 28
It's all going to be primitive data types (or their respective classes).
I really need this to be very flexible so I'm sorry I can't give a lot of information, but I think just declaring them as object will work. Thanks.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

Tom

Do you really mean primitives here? Primitives are different to Objects. See this link for more info:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Tom Josephits
Greenhorn

Joined: Mar 11, 2012
Posts: 28
I know they're different, but they each have a class I could encapsulate them in. My point with saying primitive data was just to give the idea that it's mostly numbers and maybe a few booleans.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

Here's the thing about programming and programmers - we are INCREDIBLY literal. If you say "it has to be a primitive", then we assume that is what you mean. When you then come back and say "well, it doesn't have to be a PRIMITIVE primitive", we tend to feel like you are wasting our time.

If the values have to be numeric, then say they have to be numeric - don't say they have to be primitives, as that has a VERY specific meaning.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
Don't get me started about those stupid light bulbs.
 
subject: How could I declare variable of a type determined after the program's running?
 
Similar Threads
Type of the Generic List
An exception throws in hibernate
How is human food discovered?
Expert Level quesiton from J@Whiz
Problems view in NetBeans 5.5?