wood burning stoves*
The moose likes Beginning Java and the fly likes Using a class as a parameter in a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using a class as a parameter in a method" Watch "Using a class as a parameter in a method" New topic
Author

Using a class as a parameter in a method

Mnau Lubi
Ranch Hand

Joined: Mar 17, 2010
Posts: 32
Hi!

I am using a POJO ( a very simple class: some variables + getters and setters) to store info of bikes. For example:



Therefore, each bike is represented by one POJO.

I would like to know if it is possible to create a method which takes the class as parameter. For example:



So I can just pass one parameter (the class) instead of having to execute all the getters in that class to obtain all the variables and then call the method with thousand parameters.

Thanks in advance

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

probably, reflection mechanism fit for your requirement
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Your requirement is unclear. Can't you just pass the instance of the object?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

It's not clear what you mean exactly.
Mnau Lubi wrote:So I can just pass one parameter (the class) instead of having to execute all the getters in that class to obtain all the variables and then call the method with thousand parameters.

Do you mean that you want to set the fields of all existing instances of a certain class to certain values? There is no way in Java to find all existing instances of a class, if you only know the class. Java doesn't keep track of all the instances of a class; you have to do that yourself. So if that's what you mean, then that's not possible.

If you have all your Track objects in a collection, for example a List, you could ofcourse pass that List to your method:


Or... are you maybe confusing the terms "class" and "object", and do you simply mean that you want to pass a Track object to your method:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Mnau Lubi
Ranch Hand

Joined: Mar 17, 2010
Posts: 32
yes, sorry I was confusing class with object
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using a class as a parameter in a method