Solved

Calling setters on abstract class

Posted on 2009-07-03
1
528 Views
Last Modified: 2013-11-24
Hi,

In my code I am instantiating an abstract class need to call the setters on that class. How can I call the setters on the class in a generic form without knowing the name of the setter.

Here is an example of what  I mean:


      Class clazz = Class.forName(getClassName());
      if (clazz!=null)
        Entity entity = (Entity)clazz.newInstance();

At runtime will know the name of the field I want to set, eg "Description". I know that there will be an associated setter named setDescription(String desc). But since I will not know this until runtimes I can't code this right now. Basically in pseudocode I would would do it like this for a field named "Description".

String fieldName = "Description";

Method method = "set" + fieldName;

entity."method"(value);

But how to do this ?

I'm guessing I should be able to do this using reflection but since I have no experience with reflection I'm not sure how to proceed.

Thanks for any advice.

Chris



0
Comment
Question by:ChrisOz2008
1 Comment
 
LVL 26

Accepted Solution

by:
ksivananth earned 500 total points
ID: 24771603
Method[] methods = clazz.getMethods() will return all the methods

methods[0].getName() will give the name of the method, you can check if the name start with "set".

then invoke the method,

methods[0].invoke( entity, yourValue ) ;
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Exception after setting jdbc session management 2 52
topping2 challenge 13 83
Java Jpanels and Jframe 8 28
eclipse java  build path 6 44
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now