• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tom Josephits
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic