Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using a class as a parameter in a method

 
Mnau Lubi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably, reflection mechanism fit for your requirement
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your requirement is unclear. Can't you just pass the instance of the object?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Mnau Lubi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, sorry I was confusing class with object
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic