1

I'm new to java so I'm asking this although thing here might be obvious. Whats the real life usage of Java Beans. They are defined as reusable software component ,but only examples I could find use them as POJO in Jsp and Swing programing. Is it valid scenario to define bean that will do some complex calculation having user to set bean "properties" as parameters of that calculation? Can they expose other types that are not beans , primitive types trough their properties?

user629926
  • 1,910
  • 2
  • 18
  • 43

2 Answers2

1

Practically, JavaBeans are regular Java-classes following naming conventions for "property" access methods - getters and setters. I think you understand it correctly.

Here is more detailed answer: What is Java Bean

Community
  • 1
  • 1
Vasyl Keretsman
  • 2,688
  • 2
  • 18
  • 15
1

Usually Beans hold informations only. Some basic calculations ok like this one:

private int age=17;
public int getAge(){return age;};
public boolean isOver18(){return getAge() > 18;};
public boolean isNotOver18(){return getAge() < 19;};
public boolean isSurfable(){
    int minAge = beachService.findBeachBean().getMinimumAge();
    int maxAge = beachService.findBeachBean().getMinimumAge();
    boolean hasPhysicalProblems = humanService.findAnatomyBean().hasLegs();
    if(!isSurfboard()){
        hasPhysicalProblems = true;
    }
    return isOver18() && getAge() < maxAge && getAge() >= minAge && hasPhysicalProblems;
}

(Btw. it is important to use getAge() in isOver18() because of Mocking and Inheritation)

See, if you stop a webserver and you got a user logged-in. The session-passivation will easyly save the loginbean to server's HDD for a specific sessionid (8AHE765F7655F765), and reactivate the Bean after the server has been restarted and the session try to reactivate this loginbean from HDD. So the beans should always be specific to a State or an Action.

Grim
  • 1,938
  • 10
  • 56
  • 123
  • See documentation of readObject for passivation also on more complex fields like "int age"! – Grim Mar 11 '13 at 13:38
  • So I can forget about thing like calling web service from java bean? – user629926 Mar 11 '13 at 14:43
  • You can call webservices in Enterprise Java Beans. You even must call webservices in wsdl2java-generated Java-beans. BTW, you tagged this question as a SWING-Question, you better not call webservices in a client-app. – Grim Mar 11 '13 at 16:18