Solved

Calling setters on abstract class

Posted on 2009-07-03
1
532 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
varialbe initialization 11 37
how to install java on RHEL image on EC2 4 39
pagenation logic how it is working in my code 1 52
how to debug htl and js pages 8 38
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

792 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