Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Object type argument? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object type argument?" Watch "Object type argument?" New topic
Author

Object type argument?

sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear friends,
I have to pass many parameters using one argument with data type "object". how can I do this?
how can I pass many parameters just with one Object type argument? can I do somthing with it, like reading one String and then break it to desired section and put it in desired parameters?
or do you have any better idea?

thank you in advance,
Sahar.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
You can create a new class that has fields (with getter and setter methods) for all those parameters. Of course, the receiving method will have to cast the Object to its actual class. It might be better to change the signature of the method so that it uses the actual class.


Ping & DNS - updated with new look and Ping home screen widget
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
You can pass many arguments by incorporating them into some sort of data object, but that would not have the type "Object". What would you do it an Object of a different type is passed? You are losing compile-time checking by using "Object" as a parameter type.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Is there any particular reason you want to pass an Object ?


[My Blog]
All roads lead to JavaRanch
Ranveer K Kumar
Ranch Hand

Joined: Sep 13, 2009
Posts: 64
of course you can do by List or Map.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Christophe Verré wrote:Is there any particular reason you want to pass an Object ?
Probably because some has written a method with "Object" as its parameter type, not thinking what would be passed.
sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear friends,
Yes, I want to use a method called creatNewAgent which is as follow:
creatNewAgent(String nickName, String className, Object [] args);
and I need to pass a sort of different types of variables like: Price(double), Strategy(String),Type(String), Utility(double) by arguman args.

1) dear Cristophe yes I have to use this pre-defined method and it needs an Object
2) dear Ritchie I did not understand what you mean exactly? it means that I have to incorporate some sort of data types but still its not an object? so, how can I use it? could you please explain more? I didn't get how can I solve it

thank you so much!
Sahar.
sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear friend,
Any idea or example would be appreciated, after 20 days I still couldn't solve it. I also face with another method putO2AObject(Object,boolean). Here, also I need to pass a number of different types of variable to this methos as an object(which i dont know how?). I've tried to define a constructor and declare all this variables in that constructor, then make a new isntance of that constructor. But still I dont know how to convert this constructor instance to an object (so it could be passed to PutO2AObject())?
here is the code for:
constructor:



and piece of code which Im using pre-defined putO2AObject class :



but its not working.
any idea would be really appreciat!
Thank you in advance,
Sahar.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Yes, I want to use a method called creatNewAgent which is as follow:
creatNewAgent(String nickName, String className, Object [] args);
and I need to pass a sort of different types of variables like: Price(double), Strategy(String),Type(String), Utility(double) by arguman args.


What have you tried? It seems pretty straightforward to me...



Now, of course, the next issue is... is this what the method expects? Calling it is one thing, but the method needs to know what to do with it too. In other words, it may be a better idea to ask whoever provided this creatNewAgent() method, to explain exactly how to use it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Object type argument?
 
Similar Threads
How do we talk about class for a return value or a parameter?
How does this output comes?
confused with generics
maximum number of parameters...
Need to resolve ClassCastException