Chapter 7: Making JavaBeans - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Chapter 7: Making JavaBeans

Description:

Must have a zero-argument constructor. Used when restoring JavaBean from a file ... Can save JavaBean to a file. Private instance variables ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 9
Provided by: conest
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7: Making JavaBeans


1
Chapter 7Making JavaBeans
Reference Building J2EE Apps with WebSphere
2
Objectives
  • Understand JavaBeans basics
  • Properties
  • Methods
  • Events
  • Bean Types
  • Building JavaBeans in WSAD

3
What Are JavaBeans?
  • Javas software component model specification
  • Written in Java platform neutral
  • JavaBean Conventions
  • Must have a zero-argument constructor
  • Used when restoring JavaBean from a file
  • Must be serializable
  • Implements java.io.Serializable
  • Can save JavaBean to a file
  • Private instance variables
  • Public features (methods, properties, events)

4
JavaBean Property Types
  • Reference Page 131 - 133
  • Simple
  • Use standard getter/setter signatures
  • E.g., public String getName()
  • Boolean
  • Use isltPropertyNamegt for getter method
  • E.g., public boolean isValid()
  • Indexed
  • Represents an array of values
  • Use java.util.ArrayList for dynamic arrays

5
Simple JavaBean Sample
  • WSAD 5.1 can generate getter/setter methods for
    you.
  • public class User implements java.io.Serializable
  • private String name
  • public User()
  • super()
  • public String getName()
  • return name
  • public void setName(String string)
  • name string

6
Indexed JavaBean Sample
  • public class UserList implements
    java.io.Serializable
  • java.util.ArrayList userList
  • public UserList()
  • super()
  • // generated methods
  • // get/set entire ArrayList
  • public java.util.ArrayList getUserList()
  • return userList
  • public void setUserList(java.util.ArrayList
    list)
  • userList list
  • // manually added methods
  • // get/set an element of an ArrayList
  • public User getUserList(int index)
  • return (User) userList.get(index)
  • public void setUserList(User aUser, int index)

7
JavaBean Types
  • Reference Pages 131 - 140
  • GUI Beans
  • E.g., Button, TextField, JFrame
  • Not the focus of this course
  • Invisible Beans
  • Used mainly as data holders
  • May contain application logic (e.g., validation)
  • WSAD Support Can add JavaBeans to JSP

8
Building JavaBeans
  • Reference Pages 142 151
  • WSAD 5.1 has wizards to build JavaBeans
  • Class Wizard
  • Remember to implement java.io.Serializable
  • Generate Getter/Setter methods
  • Refactoring
  • Lazy Initialization
  • Always return a valid object and not a null
    reference.
Write a Comment
User Comments (0)
About PowerShow.com