eWebProgrammer eweb  





Java Beans   «Prev  Next»
Lesson 1

Deeper inside a Bean

How does introspection expose the internals of JavaBeans?

This module continues with the study of Bean structure by examining introspection and how Beans expose information about themselves.
You learn about the two different approaches to introspection supported by JavaBeans: automatically using design patterns or explicitly using a special class in the JavaBeans API.
The module culminates in the complete development of a basic Bean, which draws upon much of what you have learned throughout the entire course thus far.

Module learning objectives
After completing the module, you will have the knowledge and skills necessary to:
  1. Apply property and event design patterns to support automatic introspection
  2. Explicitly provide Bean information in lieu of automatic introspection
  3. Build a basic Bean from scratch
In the next lesson, you learn how Beans expose their internal structure and functionality.

Introductory Quiz Question on JavaBeans

Which of the given statements are true, if a JavaBean has the string property name, on which reading and writing are allowed? (Choose two)
  1. The getter and setter methods must be public.
  2. For boolean properties, the setter method must always start with "is".
  3. The setter method must have the property type as the return type.
  4. The setter method must have the property type as the argument type.
  5. The getter method must have the property type as the argument type.


Answer: A, D
Choices A and D are the correct answers.
According to the JavaBeans naming standards, if the property name is 'x' and the type is Type, the accessor method is of the form:
public Type getX() 
and the mutator method is of the form:
public void setX(Type newValue) 
However, boolean property uses another convention as the following as well:
public boolean isX() 
public void setX(boolean newValue) 
For read/write properties, both getter and setter methods must be public, so choice A is correct.
Properties of boolean type may use getter methods starting with "get" or "is". So choice B is incorrect. The setter method must have the property type as the argument type, while the getter method must have the property type as the return type. Hence, choice D is correct, while choices C and E are incorrect.