• 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

JavaBean Doubt?

 
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is from David Bridgewater's book,

But let�s look at the next pair of methods�setWeight() and getWeight(). From this, we infer there is a property called weight. And this is correct�even though the instance variable connected to these methods is called something quite different: mass, in this case. How we represent the data in the bean (and we may not bother at all) doesn�t matter.

I'm not understanding the last 3 lines above?
 
Joe Harry
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it mean that the get and set methods should exactly match the variables which otherwise we are violating JavaBean specifications?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How we represent the data in the bean (and we may not bother at all) doesn�t matter.


He means that it not necessary to follow the naming rule for beans' getters and setters. Usually, you have weight -> getWeight, setWeight.

But you can have :
[code]
int mass;

public int getWeight() {
return mass;
}

public void setWeight(int weight) {
mass = weight;
}
[code]
and you can still access the mass via ${mybean.weight}
 
Joe Harry
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how can I access mass like ${mybean.weight}? won't it complain about Canot find symbol?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


But how can I access mass like ${mybean.weight}? won't it complain about Canot find symbol?



I don't think, it will complaint. It finds the getter method named getWeight.
That's it.

Thanks,
 
Joe Harry
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok the EL works that way...am I right?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ok the EL works that way...am I right?


Yes. But I don't find it a good idea to give different names for getters and setters. I think it's better to follow the java beans naming rule anyway.
 
Joe Harry
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satou,

If I don't use EL, what will be the o/p if I try to access that with an object?? It should throw error. I'm right or wrong?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I think it will be ok. I think that properties are accessed via getter/setters anyway, so as long as you give the correct name for getters/setters, it's ok.
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic