File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply 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: 27
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: 5868
    
    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: 27
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
Ranch Hand

Joined: Nov 09, 2011
Posts: 657
    
    2

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: 27
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: 9948
    
    6

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.


Never ascribe to malice that which can be adequately explained by stupidity.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How could I declare variable of a type determined after the program's running?
 
Similar Threads
Expert Level quesiton from J@Whiz
Type of the Generic List
Problems view in NetBeans 5.5?
An exception throws in hibernate
How is human food discovered?