Creating Object With Methods During Runtime  
Author Message
A_W





PostPosted: 2004-2-21 9:31:00 Top

java-programmer, Creating Object With Methods During Runtime Hello All,

Is there a way to have a method create and name an object?

If my question is not clear, what I mean is instead of this:

...
Person abc = new Person();
...

where the programmer specifies the name of the object and where in the
program it is created, you have a method something like this:

...
//the string that is passed to this method is generated by another
//function which generates random strings - in other words, neither
//the programmer nor the user are involved in this step directly

private void createObject(String s)
{
Person s = new Person();
}
...

This might be used in a GUI simulation that is modeling the interaction
of different species in an ecosystem, for example. At some point the
user may wish to increase the complexity of the sim by adding more
creatures of one type or another. They could press a button which would
call the createObject function and insert another animal into the fray.

I hope that is enough to sufficiently explain what I am asking, in any case.

By the way, just in case you are wondering, I have not tested the
example function above (I know it won't work as is). Normally I would
try myself before asking here, but I don't have access to my PC right
now and I can't find what I'm looking for on the internet.

Thanks very much to any who can help.


A


 
Ryan Stewart





PostPosted: 2004-2-21 11:58:00 Top

java-programmer >> Creating Object With Methods During Runtime "A_W" <email***@***.com> wrote in message
news:kuyZb.16428$email***@***.com...
> Hello All,
>
> Is there a way to have a method create and name an object?
>
> If my question is not clear, what I mean is instead of this:
>
> ...
> Person abc = new Person();
> ...
>
> where the programmer specifies the name of the object and where in the
> program it is created, you have a method something like this:
>
> ...
> //the string that is passed to this method is generated by another
> //function which generates random strings - in other words, neither
> //the programmer nor the user are involved in this step directly
>
> private void createObject(String s)
> {
> Person s = new Person();
> }
> ...
>
> This might be used in a GUI simulation that is modeling the interaction
> of different species in an ecosystem, for example. At some point the
> user may wish to increase the complexity of the sim by adding more
> creatures of one type or another. They could press a button which would
> call the createObject function and insert another animal into the fray.
>
> I hope that is enough to sufficiently explain what I am asking, in any
case.
>
> By the way, just in case you are wondering, I have not tested the
> example function above (I know it won't work as is). Normally I would
> try myself before asking here, but I don't have access to my PC right
> now and I can't find what I'm looking for on the internet.
>
> Thanks very much to any who can help.
>
>
> A
>
My first thought is you probably need to spend some time looking over the
tutorials at the Sun site:
http://java.sun.com/docs/books/tutorial

As for what you want to do, I don't know if it's possible, but I don't think
it's really what you want anyhow. If you're adding creatures to an
ecosystem, then you have an ArrayList called "ecosystem" and when they click
a button, you add a new Creature to that ecosystem:
ArrayList ecosystem = new ArrayList();
.
.
.
public void actionPerformed(ActionEvent ae) {
if (...) {
ecosystem.add(new Creature());
}
}


 
Steve Horsley





PostPosted: 2004-2-21 20:27:00 Top

java-programmer >> Creating Object With Methods During Runtime A_W wrote:
> Hello All,
>
> Is there a way to have a method create and name an object?
>
<snip>
No. What you are calling "name" is just a reference to an object. The
name is just so you can keep track of that reference and use/assign it.
It has nothing whatsoever to do with whatever object it currently refers
to. Consider...

Object aaa = new Object();
Object bbb = aaa;
Object ccc = bbb;
aaa = null;

What is the object's name after this? is it bbb, ccc, both or neither?

And how about this...

List myStuff = new ArrayList();
myStuff.add(new Object());

What is the object's name now? It doesn't seem to have one!

An object's name (in this sense) is just a label someone might use to
refer to it, and it is not a property of the object at all. In the same
way that I am know as "my husband", "my Dad", "that bloke there", "my
brother", "the chap next door", "big boy" (in my dreams), and of course,
"me".

Steve
 
 
A.Buschmann





PostPosted: 2004-2-22 0:14:00 Top

java-programmer >> Creating Object With Methods During Runtime "A_W" <email***@***.com> schrieb im Newsbeitrag
news:kuyZb.16428$email***@***.com...
> If my question is not clear, what I mean is instead of this:
>
> ...
> Person abc = new Person();
<snip>
>private void createObject(String s)
>{
> Person s = new Person();
>}
>

Hello,

I think what you want to do is storing the user defined name of each
Object-Instance in the Instance.
So it should be:
public class Person {

private String myName;

private Person(){
// private if you don't want to have a Person without name,
otherwise public too
myName = "";
}

public Person(String name) {
myName = name;
}

public void setName(String name) { myName = name);
public String getName() { return name };

}

Somewhere else:

private void createObject(String s)
{
Person abc = new Person(s);
}

Now you can use the instance, which is right now abc (and may later on get
other names) use, and if you want to see, wich Person you are accessing,
type
abc.getName();

Alex