Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Different instance variable and local variable names in Entity class

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am following basic example in Spring. The program is about printing a message "Hello World".

My question is : Can I use a different name for the instance variable and local variable in the Hello World Entity class? When I use the same name it works fine. But when I chose instance variable name as message and local variable name as msg as shown below it gives an error.





 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can, but the property name in the XML refers to the name of the instance variable, not the name of the local variable.

So if the instance variable is named "message", then you must use <property name="message"> in the XML. It doesn't matter what the name of the local variable is.
 
Suhaas Mohandos
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. It worked. But now my question is the setter is taking a local variable as a parameter then how come the property mentioned in the xml is of instance variable?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The HelloWorld bean has a property (a field) named 'message', and a corresponding setter method, 'setMessage'. The property name in the XML file refers to the name of the property in the HelloWorld bean, which is 'message'; not to the name of the argument variable.

Spring looks at the XML and sees that you want to set a property named 'message', so it looks for the 'setMessage' method in the HelloWorld bean, and then calls it with the value specified in the XML file.

If you'd name the property 'msg' in the XML file, it would go looking for a 'setMsg' method, and then it finds that this does not exist and throws an exception.
 
Suhaas Mohandos
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok..thanks
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic