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 create objects of my classes dynamically and put them into linkedlist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "create objects of my classes dynamically and put them into linkedlist" Watch "create objects of my classes dynamically and put them into linkedlist" New topic
Author

create objects of my classes dynamically and put them into linkedlist

Sheila Jorge
Greenhorn

Joined: Oct 24, 2011
Posts: 16
Hi all,

I have two classes in my code, Manager class and Laborer.

I have another class in same code named Producer. In this class i should make objects of Manager and Laborer classes dynamically and put them into a linked list of Employee type.

I don't know what can use to make objects dynamically ?
can i use Class.forName();?

PLEASE GUIDE ME
THANKS A LOT
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
would you mind pasting the code here? I think Manager and Laborer classes extend employee class?
Sheila Jorge
Greenhorn

Joined: Oct 24, 2011
Posts: 16
Harsha Smith wrote:would you mind pasting the code here? I think Manager and Laborer classes extend employee class?



Yes ,exactly. Manager class is :



Do i write this code correctly?

[Edit - added code tags - MAB]
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
What do you mean by dynamically create objects of type Employee? and also the code is not complete
Sheila Jorge
Greenhorn

Joined: Oct 24, 2011
Posts: 16
I must make object of Manager and Laborer classes that extends Employee .( not making object of Employee ) these objects ( of manager and laborer class) must make dynamically .
I don't know how can i create object of Manager and Laborer dynamically in data() method.



[Edit - added code tags - MAB]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4375
    
    8

What's wrong with new Manager()? There doesn't seem to be a strong reason to use reflection here (since the class name is hard-coded anyway).

Except...in the Manager class, you're creating a new Manager as an instance variable. Which means that the moment you create one, your code is going to go into an infinite loop and you'll get a StackOverflowException.
Sankar Mitra
Greenhorn

Joined: Oct 24, 2011
Posts: 4

Your thinking process is correct.Is'nt it working??
Sheila Jorge
Greenhorn

Joined: Oct 24, 2011
Posts: 16
I think my code is incorrect . I didn't test it yet. I only want to know how can we make objects of classes dynamically ?!!!I just know it.
don't have any person any idea about it !!!???!!!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4375
    
    8

But what do you mean by that? It's not clear.

For instance, based on your code above, there's no reason not to do this:

That's "dynamically creating them" (but maybe not in the way you mean). So, is there some reason that you can't do that? What other requirements do you have?
Sheila Jorge
Greenhorn

Joined: Oct 24, 2011
Posts: 16
I think your idea is correct .

I search for creating dynamically object and i found something that i wrote in my code(using of Class.forName() ...) otherwise I don't have any idea about creating dynamically object myself.

I must write a program that in this there must be an abstract base class named Employee.It have two method data() and displayData() . we have another classes named Manager and Laborer that extends Employee. these classes must implement data() and displayData() methods too.

we have another class named Producer that it have data() method too. In this method we must make objects of Manager and Laborer class dynamically and save them into a linked list so that if data() method get 'm' make object of Manager if it get 'l' make object of Laborer class .

I think you can understand my intention now better.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I think that if you removed the word "dynamically" from your vocabulary, things would be a lot easier. People throw that word around all the time without really saying (or knowing) what they mean by it. Often they mean something like "at run time in a complicated way", so then using it is like throwing dust in everybody's eyes.

Ask yourself "What would it mean for me to do this non-dynamically?" and you might see what I mean. If you can't explain what that means, then you ought not to use the word at all.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Why have you got a Manager field in the Manager class? That looks mistaken to me.
Sheila Jorge
Greenhorn

Joined: Oct 24, 2011
Posts: 16
Campbell Ritchie wrote:Why have you got a Manager field in the Manager class? That looks mistaken to me.


In Employee class that is base and parent class there is Data() method . In Manager class i want call it and get data such name, lastname so i make an object of manager and write manager.data()

you are right it is mistake and i get Stackoverflow error.

how can i create an object of Manager in Producer class so that it call data() method in Employee?!!!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7713
    
  20

Sheila Jorge wrote:how can i create an object of Manager in Producer class so that it call data() method in Employee?!!!

Matthew is right; you really need to think through what you're trying to do and explain it so that we can understand. Most of us here have had a reasonable amount of experience, so if you're having trouble getting it across, I suspect the fault lies with you; so please, no more multiple question and exclamation marks.

I will tell you one thing for free: Class.forName() is probably NOT what you want. It's tricky, and SLOW.

In Employee class that is base and parent class there is Data() method . In Manager class i want call it and get data such name, lastname so i make an object of manager and write manager.data()

Then do it. Providing the Manager class does not override Employee's data() method, then instances of Manager will execute it. If, however, it does, and you want to run Employee's data() method from inside Manager's, use:
super.data()

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Sheila Jorge
Greenhorn

Joined: Oct 24, 2011
Posts: 16
Thanks Winston my problem solved with your help
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
Campbell Ritchie wrote:Why have you got a Manager field in the Manager class? That looks mistaken to me.

Do you mean to have a hierarchy? like a manager has a manager..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Gaurav Raje wrote: . . . Do you mean to have a hierarchy? like a manager has a manager..
. . . and a Manager has a Manager and a Manager has an infinite recursion . . .
 
Don't get me started about those stupid light bulbs.
 
subject: create objects of my classes dynamically and put them into linkedlist