Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7751151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:28:27+00:00 2026-06-01T11:28:27+00:00

Well I am trying to learn the java MVC pattern, but I can’t understand

  • 0

Well I am trying to learn the java MVC pattern, but I can’t understand the following method:

protected void setModelProperty(String propertyName, Object newValue) {

        for (AbstractModel model: registeredModels) {
            try {

                Method method = model.getClass().
                    getMethod("set"+propertyName, new Class[] {
                                                      newValue.getClass()
                                                  }
                             );
                method.invoke(model, newValue);

            } catch (Exception ex) {
                //  Handle exception
            }
        }
    }

I dont’ understand the:

Method method = model.getClass().
                    getMethod("set"+propertyName, new Class[] {
                                                      newValue.getClass()
                                                  }
                             );

So in getMethod we are retrieving the (setSomething) method name according to the property, then the following “thing” is the property value as newValue which is expressed in this fancy expression that I don’t understand at all.

new Class[] <— So it’s an array of classes???
next { newValue.getClass() } <—- OK, getting the class name in brackets by calling the method, but what about the semicolon? There must be some special structure that I don’t understand, it looks like a class, but that must be something different if there’s no semicolon… People explain me what this is please…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:28:28+00:00Added an answer on June 1, 2026 at 11:28 am

    This is a bit of an abstract way of coding, and to be honest I’d discourage this way of working if it’s possible to do it otherwise (e.g. by using templates), especially at beginners level. Anyway, I’ll try to explain.

    All classes in Java are also objects of the class Class. And all methods are objects of the class Method. You can manipulate these objects like any other objects, by calling their methods and using them as arguments for other methods and so on. That way, you can perfectly instantiate a class by only knowing its name as a String. Same for methods: you can call a method by simply knowing its name as a String.

    Now let’s look at your code. Have a quick look at this entry in the Java API for a brief explanation of this part getMethod("set"+propertyName, new Class[] {newValue.getClass()});.

    Say you want to call the method setParameter(int parameterValue) {...}. In that case, we call your method with propertyName set to "Parameter" and newValue set to a certain integer 123. Now "set"+propertyName results in setParameter, which is the name of our method. newValue.getClass() gives Integer, since that’s what 123 is.

    The getMethod requires an array of Classes though, because there may exist a number of methods with the same name, but a different number and types of arguments (e.g. there is a chance the method setParameter(double parameterValue) {...} exists as well). So we put that newValue.getClass() in an array with only one item, by writing new Class[] {newValue.getClass()}.

    And there you have it: you retrieve a Method object by calling

    Method method = model.getClass(). getMethod("set"+propertyName, new Class[] {newValue.getClass()});

    and then you call that method using method.invoke(model, newValue);, which is simply a dynamic way of calling setParameter(123).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to learn Java, Hibernate and the MVC pattern. Following various tutorial
Well, I am beginner in Android and java, but I'm trying to learn. My
i know virtually no java but i'm trying to learn some for this project.
I'm trying to learn about regular expressions but am not doing so well after
I'm making my first Java project, and trying to learn OO programming as well
Well i'm a ruby newbie and im trying to learn with RubyKoans but i
I'm trying to learn Ruby as well as Ruby on Rails right now. I'm
Well, I am trying to use an element of function list.But i am having
I am novice to maven (as well as to hibernate) but want to learn
I'm trying to learn the new cfscript syntax, as well as use cflogin. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.