Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
JavaBeans
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== JavaBean conventions == In order to function as a JavaBean [[Class (computer science)|class]], an object class must obey certain conventions about method naming, construction, and behaviour. These conventions make it possible to have tools that can use, reuse, replace, and connect Java Beans. The required conventions are as follows: * The class must have a public [[default constructor]] (with no arguments). This allows easy instantiation within editing and activation frameworks. * The class [[property (programming)|properties]] must be accessible using ''get'', ''set'', ''is'' (can be used for boolean properties instead of get), ''to'' and other methods (so-called [[Accessor|accessor methods]] and [[mutator method]]s) according to a standard [[naming conventions (programming)|naming convention]]. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties. Setters can have one or more arguments. * The class should be [[Serialization#Programming language support|serializable]]. (This allows applications and frameworks to reliably save, store, and restore the bean's state in a manner independent of the [[Virtual machine|VM]] and of the platform.) ===Code example=== <syntaxhighlight lang=Java> package player; public class PersonBean implements java.io.Serializable { /** Properties **/ private boolean deceased = false; private List list; /** Property "name", readable/writable. */ private String name = null; /** No-arg constructor (takes no arguments). */ public PersonBean() { } public List getList() { return list; } public void setList(final List list) { this.list = list; } /** * Getter for property "name". */ public String getName() { return name; } /** * Setter for property "name". * * @param value */ public void setName(final String value) { this.name = value; } /** * Getter for property "deceased" * Different syntax for a boolean field (is vs get) */ public boolean isDeceased() { return deceased; } /** * Setter for property "deceased". * @param value */ public void setDeceased(boolean value) { deceased = value; } } </syntaxhighlight> '''<u><code>TestPersonBean.java</code></u>''': <syntaxhighlight lang=Java> import player.PersonBean; /** * Class "TestPersonBean". */ public class TestPersonBean { /** * Tester method "main" for class "PersonBean". * * @param arguments */ public static void main(final String[] arguments) { final PersonBean person = new PersonBean(); person.setName("Bob"); person.setDeceased(false); person.setList(new ArrayList()); // Output: "Bob [alive]" System.out.print(person.getName()); System.out.println(person.isDeceased() ? " [deceased]" : " [alive]"); } } </syntaxhighlight> <syntaxhighlight lang="xml"> <jsp:useBean id="person" class="player.PersonBean" scope="page"/> <jsp:setProperty name="person" property="*"/> <html> <body> Name: <jsp:getProperty name="person" property="name"/><br/> Deceased? <jsp:getProperty name="person" property="deceased"/><br/> <br/> <form name="beanTest" method="POST" action="testPersonBean.jsp"> Enter a name: <input type="text" name="name" size="50"><br/> Choose an option: <select name="deceased"> <option value="false">Alive</option> <option value="true">Dead</option> </select> <input type="submit" value="Test the Bean"> </form> </body> </html> </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)