This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!