• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaBean Doubt?

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC 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: 10128
3
Eclipse IDE Mac PPC 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?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 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: 10128
3
Eclipse IDE Mac PPC 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?
 
Chandra Bhatt
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: 10128
3
Eclipse IDE Mac PPC 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 Ubuntu VI Editor
  • 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: 10128
3
Eclipse IDE Mac PPC 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 Ubuntu VI Editor
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic