Declaring variables dynamically

R

rshibli

Hello,
I am new Java programmer and I am trying to declare objects
dynamically. That is, class names (object types) are saved in memory
(lets say array) and at run time I need to declare objects of the types
saved in the array.
lets assume we have an array
a[1]=x and a[2]=y //x and y are class names imported
for i =0 to a.length
a Oi;

Thanks
 
B

Bjorn Abelli

I am new Java programmer

Hence a more appropriate group would be comp.lang.java.help...
and I am trying to declare objects
dynamically. That is, class names (object types)
are saved in memory (lets say array) and at run
time I need to declare objects of the types
saved in the array.
lets assume we have an array
a[1]=x and a[2]=y //x and y are class names imported
for i =0 to a.length
a Oi;


What you need to read up on is "reflection". I'll give a small example from
which you possibly can elaborate upon further.

public class ReflectionSample
{
public static void main(String[] args)
{
String aClass = "java.util.Date";

try
{
Class theClass = Class.forName(aClass);

Object o = theClass.newInstance();

System.out.println(o);

}
catch (ClassNotFoundException cex)
{
System.err.println (aClass + " not found");
}
catch (InstantiationException cex)
{
System.err.println (aClass + " could not be instantiated (e.g.
abstract)");
}
catch (IllegalAccessException cex)
{
System.err.println (aClass + " could not be instantiated (e.g. no null
constructor)");
}
}
}

--------------------

Look further into the class Class, and then the package java.lang.reflect,
for more ideas of how to do what you want...

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html

http://java.sun.com/j2se/1.5.0/docs/api/index.html?java/lang/reflect/package-summary.html


// Bjorn A
 
J

John C. Bollinger

I am new Java programmer and I am trying to declare objects
dynamically.

That doesn't make sense. A declaration is a statement in the source
code, and it has no direct representation at the bytecode level, just as
a C declaration has no direct representation in compiled object code.
That is, class names (object types) are saved in memory
(lets say array) and at run time I need to declare objects of the types
saved in the array.

You can instantiate objects whose classes you do not know at compile
time. Look at Class.forName() and Class.newInstance(). If you want to
assign the reference of such an object to a variable, however, then that
variable's type must be known at compile time, typically as a superclass
of the object's class. Class Object might be the best you can do, but
an interface type or more specific class is typically more useful, when
applicable.
lets assume we have an array
a[1]=x and a[2]=y //x and y are class names imported
for i =0 to a.length
a Oi;


No, you cannot do anything like that, and chances are good that there
are much better ways of achieving what you are actually trying to
accomplish. And what is that, anyway?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top