Creating an object during runtime

P

Peter Jones

I'm trying to generate an object name during run time, and then create an
object with the contents of that variable. You can see that I'm trying to
also make the object name self generating (almost, the 'y' variable isnt
being incremented yet), putting two values ('y' and 'C'). My apologies for
poor coding structure / lack of comments, but I'm am a bit lost on how to
work it out. MANY thanks in advance.Heres what I have so far:

public class testVariable
{
private String name = "";
private String New_name = "";
private int y = 2;

public testVariable()
{}

public void test(String x)
{
name = x;
New_name = name + y ;
System.out.println(New_name);
}

public String getNew_name()
{
return New_name;
}



public static void main(String[] args)
{
int z = 2;
testVariable Darren = new testVariable();
Darren.test("C");
testVariable "c"+ z = new testVariable();
String Old_name = New_name;
}
}
 
T

Tjerk Wolterink

Peter said:
I'm trying to generate an object name during run time, and then create an
object with the contents of that variable. You can see that I'm trying to
also make the object name self generating (almost, the 'y' variable isnt
being incremented yet), putting two values ('y' and 'C'). My apologies for
poor coding structure / lack of comments, but I'm am a bit lost on how to
work it out.

I cannot help you if you don't document your code.
 
Z

zero

I'm trying to generate an object name during run time, and then create
an object with the contents of that variable. You can see that I'm
trying to also make the object name self generating (almost, the 'y'
variable isnt being incremented yet), putting two values ('y' and
'C'). My apologies for poor coding structure / lack of comments, but
I'm am a bit lost on how to work it out. MANY thanks in advance.Heres
what I have so far:

public class testVariable

Please start class names with a capital letter.
{
private String name = "";
private String New_name = "";

Please don't start variable names with a capital letter.
private int y = 2;

public testVariable()
{}

public void test(String x)
{
name = x;
New_name = name + y ;
System.out.println(New_name);
}

public String getNew_name()
{
return New_name;
}



public static void main(String[] args)
{
int z = 2;
testVariable Darren = new testVariable();

Please don't start variable names with a capital letter.
Darren.test("C");
testVariable "c"+ z = new testVariable();

This line can't possibly compile correctly. What are you trying to do
here?
String Old_name = New_name;
}
}


If you want to create objects at runtime you need the reflection api.
For example, to create a new object of class C2:

Class classDefinition = Class.forName("C2");
Object object = classDefinition.newInstance();

Here object will be an instance of C2.
 
P

Peter Jones

zero said:
If you want to create objects at runtime you need the reflection api.
For example, to create a new object of class C2:

Class classDefinition = Class.forName("C2");
Object object = classDefinition.newInstance();

Here object will be an instance of C2.


Allow me to attempt to clarify. I'm trying to make an object of type class,
during runtime. The problem is, that I need to make the objectname
automatically increment as the objects are created.
e.g.
C1 C2 C3 C4 etc.
(list of created objects..)
However, I cant use a variable as a parameter in the constructor, as it uses
the variable name as the object name, not the contents of the variable.
e.g.
car = C1;
vehicle car = new vehicle();
In the above line, the new object I have created is "car" and not C1. I need
it to be C1. Your reference to the reflection API has been helpful, but I
dont see where I can use a variable of somekind, or other similar
mechanism, to let me create objects with a new object name, incremented
automatically.
again, Many Thanks in advance
 
S

Stefan Schulz

If you want to create objects at runtime you need the reflection api.
For example, to create a new object of class C2:

Class classDefinition = Class.forName("C2");
Object object = classDefinition.newInstance();

Here object will be an instance of C2.

Just to nitpick: You create all your objects at runtime, no matter if you
use reflection, or not.

Back on topic:

As far as i can see, the original author wants to create _classes_ at
runtime, for which there are some libraries available, but nothing in the
standard API... though i doubt that this is the solution to the OPs
question. Some more details about what he is trying to do (as opposed on
how he tries to do it) might help.
 
Z

zero

Allow me to attempt to clarify. I'm trying to make an object of type
class, during runtime. The problem is, that I need to make the
objectname automatically increment as the objects are created.
e.g.
C1 C2 C3 C4 etc.
(list of created objects..)
However, I cant use a variable as a parameter in the constructor, as
it uses the variable name as the object name, not the contents of the
variable. e.g.
car = C1;
vehicle car = new vehicle();
In the above line, the new object I have created is "car" and not C1.
I need it to be C1. Your reference to the reflection API has been
helpful, but I dont see where I can use a variable of somekind, or
other similar mechanism, to let me create objects with a new object
name, incremented automatically.
again, Many Thanks in advance

Ah, that's a different story. So you want something like:

SomeClass c1 = new SomeClass();
SomeClass c2 = new SomeClass();
SomeClass c3 = new SomeClass();
....

where the names c1, c2, ... are computed at runtime, correct?

Hmm... *thinking*...

I don't think this is possible. I see two possible alternatives.
Neither is exactly what you want though.

First, you could use an array.

SomeClass c[] = new SomeClass[10];
for(int i = 0; i < 10; i++)
c = new SomeClass();

Here you could see your variable names as being c[0], c[1], ... instead
of c1, c2, ...

Second, you could create a wrapper class that stores one object along
with it's name.

class WrapperClass
{
String varName;
Object varData;

public WrapperClass(String name, Object data)
{
varName = name;
varData = data;
}
}

then use this as follows:

for(int i = 0; i < 10; i++)
{
WrapperClass w = new WrapperClass("c" + i, new Object());
// use w
}

here varName would be your variable name, and varData the object.

That brings me to a third alternative: just use a Map. Maps do pretty
much the same as WrapperClass, except they have more than one name-data
pair.

Hope this helps.

zero
 
P

Peter Jones

zero said:
Allow me to attempt to clarify. I'm trying to make an object of type
class, during runtime. The problem is, that I need to make the
objectname automatically increment as the objects are created.
e.g.
C1 C2 C3 C4 etc.
(list of created objects..)
However, I cant use a variable as a parameter in the constructor, as
it uses the variable name as the object name, not the contents of the
variable. e.g.
car = C1;
vehicle car = new vehicle();
In the above line, the new object I have created is "car" and not C1.
I need it to be C1. Your reference to the reflection API has been
helpful, but I dont see where I can use a variable of somekind, or
other similar mechanism, to let me create objects with a new object
name, incremented automatically.
again, Many Thanks in advance

Ah, that's a different story. So you want something like:

SomeClass c1 = new SomeClass();
SomeClass c2 = new SomeClass();
SomeClass c3 = new SomeClass();
...

where the names c1, c2, ... are computed at runtime, correct?

Hmm... *thinking*...

I don't think this is possible. I see two possible alternatives.
Neither is exactly what you want though.

First, you could use an array.

SomeClass c[] = new SomeClass[10];
for(int i = 0; i < 10; i++)
c = new SomeClass();

Here you could see your variable names as being c[0], c[1], ... instead
of c1, c2, ...

Second, you could create a wrapper class that stores one object along
with it's name.

class WrapperClass
{
String varName;
Object varData;

public WrapperClass(String name, Object data)
{
varName = name;
varData = data;
}
}

then use this as follows:

for(int i = 0; i < 10; i++)
{
WrapperClass w = new WrapperClass("c" + i, new Object());
// use w
}

here varName would be your variable name, and varData the object.

That brings me to a third alternative: just use a Map. Maps do pretty
much the same as WrapperClass, except they have more than one name-data
pair.

Hope this helps.

zero

I have ended up using a wrapper class, its almost working as desired. I'll
post back once its ready for action.
All the best.
 
C

Chris Smith

Peter Jones said:
Allow me to attempt to clarify. I'm trying to make an object of type class,
during runtime. The problem is, that I need to make the objectname
automatically increment as the objects are created.
e.g.
C1 C2 C3 C4 etc.
(list of created objects..)

It will eventually be important for you to realize that objects simply
don't have names. For that reason, it's impossible to answer your
question of how to create objects with certain names. It would be like
asking a car salesman how to specify which color wings your new car will
come with.

What objects *do* have is references that point to them. References can
be stored in variables, which *do* have names. However, there is no way
to declare a new variable at runtime (except for, as someone pointed
out, creating a whole new class and generating Java bytecode to declare
variables in that class, then loading that class into the application at
runtime; you probably don't want to do that).

So the question is what exactly you mean by "name", and why you want to
do what you're asking for. "Zero" took some good guesses, but this
would be easier if you'd explain yourself. Chances are, using a HashMap
is the best option.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top