|
Java Programming Language Basics: JavaBean Basics
Java Bits: The Bean Builder
Program Challenge: Beans Challenge
Take an Online Quiz: Test what you learned about JavaBeans
The JavaOne Conference
For More Information: Read articles, Tech Tips, trails, and tutorials that provide more information on the topics discussed here.
Java Programming Language Basics
JavaBean Basics
Once upon a time, the world was free of naming conventions and
classes were created with methods and fields with any name
desired. Typically, everything worked, but the comprehension of
code suffered as some people liked one naming style and
other people liked other naming styles.
Naming styles are how you design your class. One naming style,
the Hungarian Naming Convention, requires you to prefix all
variable names with their data type. For example iCount would
be a variable of type int (the i) for storing a count.
The JavaBeans Component Architecture, while technically more
complicated, defines a standard naming style for the class
definitions of a JavaBean component. By following these naming
styles, you can easily package the bean components and deliver
them to users of the components.
You can package small bits of Java code called beans or
components in new ways to create unique applications, and by
following the conventions of the JavaBeans Component Architecture,
you can create reusable components for the Java platform in ways
the original designer never planned to create unique applications.
If you desire to create a reusable component for the Java
platform (or something shareable outside the platform through
such tools as the ActiveX Bridge), by following the conventions
of the JavaBeans Component Architecture, you can create these
components.
All this leads us to what those naming conventions are for
creating reusable components under the JavaBeans Component
Architecture. JavaBean components have several pieces, with
properties and events being the two biggest parts.
A property in the JavaBeans component architecture world is
defined to hold the current state of an object, in such a way
that a tool like Sun ONE Studio can access and possibly modify
that state. For instance, if the bean component to design was a
student, that student must have state information for their
name, list of classes, and prior grades, even if some people
want to forget what those grades were.
If you wanted to define a student-like object that followed the
JavaBean component model there are a handful of steps you must
do. First off you must identify the properties of the object you
wish to control. Here, for the student that might be their name,
class list, and grade list.
public class Student {
public String name;
public Subject subjects[];
public Map grades;
}
Assuming the appropriate classes were imported, this definition
would be sufficient to create a Student and store their three
bits of information: name, current subject list, and grades.
However, there is one serious flaw here. Everything is defined
to be public. After creating an instance of Student, to access
that student's name, you would need to directly access the
class' instance variable: name. If you ever wanted to change
name into a first name and last name, then you would need to
modify every place that directly accessed the variable.
Instead, the alternative is to create methods that hide the
underlying representation of the data. For instance, you could
create methods to get and set the name:
public String name()
public void name(String name)
By adding these two methods and changing the access modify of
the variable to private, you've now hidden how to fetch and
change a student's name. The naming style above has nothing to
do with the JavaBeans Component Architecture. Instead, it just
offers one way of naming the methods, one to get the value and
the other to change it.
Instead of naming methods as the variable (and property) names,
the JavaBeans Component Architecture has you name them with get
and set as the prefix. So, for the name property, those methods
look as such:
public String getName()
public void setName(String name)
By following this naming convention, a tool such as JBuilder
could examine a class and provide a graphical way to view and
modify such settings, or automate the generation of such classes
by just providing a name and data type to create the appropriate
methods. By visually programming, either by changing settings
or generating classes, the chance of programming error is
greatly reduced.
public class Student {
private String name;
private Subject[] subjects;
private java.util.Map grades;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Subject[] getSubjects() {
return subjects;
}
public void setSubjects(Subject subjects[]) {
this.subjects = subjects;
}
public java.util.Map getGrades() {
return java.util.Collections.unmodifiableMap(grades);
}
public void setGrades(java.util.Map grades) {
this.grades = grades;
}
}
And, by following the naming conventions used by the JavaBeans
Component Architecture, code comprehension improves immensely,
as new developers don't have to learn local customs to become a
productive team member.
There is one other added requirement for JavaBean components.
That is, their state must be dynamically savable. This is done
through the java.io.Serializable interface. By implementing the
interface, and having all their property variables be
Serializable, the whole class is. No methods need to be
implemented. The interface just acts as a marker.
Note: There is more to the JavaBeans Component Architecture
than just a series of naming conventions for class definitions.
There is this whole runtime containment framework that allows
you to discover the runtime context such that components
defined properly can respond appropriately whether in an applet,
application, or other environment.
Java Bits
The Bean Builder
The Bean Builder is a simple application that demonstrates new
and emerging technologies within the Java Platform which allow
the construction of applications using component assembly
mechanisms. The Bean Builder extends the capabilities of the
original BeanBox by demonstrating new techniques for persistence,
layout editing and dynamic event adapter generation.
Bean Builder demonstrates the following technologies:
- The new Long Term Persistence (LTP) mechanism for
JavaBeans. This persistence mechanism will be described
in more detail in the following section.
- Design time meta information specified in javadoc format
and generated as name-value pairs in BeanInfo classes.
- Dynamic adapter generation to hook up events using the
new Dynamic Proxy API that was introduced in Java 2
Platform, Standard Edition, release 1.3. See Dynamic
Proxy Classes for details.
- New JFC/Swing based Property Editors for editing: Fonts,
Colors, Objects, Dimension, Insets, Rectangles, and so on.
- Actions architecture based on javax.swing.Action
interface. See Adding Actions to ActionEvent Sources for
details.
- A simple Jar classloader based on java.net.URLClassLoader
that was introduced in release 1.2
- Demonstrates the use of the new javax.swing.SpringLayout
in release 1.4.
The Bean Builder requires J2SE v 1.4 or later to be installed
in order to run. The full J2SE SDK should be used rather than
the just the JRE since the Bean Builder uses the BeanInfo
design time information (dt.jar) which ships with the J2SE SDK.
Designs created by the Bean Builder can be executed with the JRE.
If you already have Java Web Start and J2SE 1.4x installed on
your system, you can start Bean Builder from The Bean Builder
Program Challenge
See a possible solution to the Challenge
Take an Online Quiz
Test what you learned about JavaBean Basics
The JavaOne Conference
If you're preparing to attend your first JavaOne Worldwide
Developers Conference, you're in for an experience you won't
soon forget. JavaOne submerges you in opportunities to learn
about and discuss anything related to Java programming and
technology.
Since the first show in 1994, the conference's general format
hasn't changed very much. Consistently, the conference is a
destination for developers interested in the Java programming
language and technologies to exchange creative concepts and
plans.
This year there are more than 55 technical sessions and
Birds-of-a-Feather (BoF) sessions for beginners. Sessions and
BoFs are the meat of the conference, full of practical
explanations of the subject and are usually presented by the
engineer(s) who helped architect or write the technical
specification.
For four days, from early morning into the wee hours of night,
your typical day includes having your senses bombarded with
input, your mind stuffed with new ideas and career options, and
your body rushed to make the next session or BoF. Start planning
now, but be prepared to make many last-minunte decisions on
which of several optional developer events to attend. The
experience can be overwhelming, and there isn't enough time for
everything you want to do - including sleep.
There's an easy way to keep track of all of the sessions and
BoFs you want to attend; "My Schedule", a feature of the JavaOne
Web site. It's on the left navigation bar under "Conference
Login". With "My Schedule" you can select all of the sessions
and BoFs you want to attend, and then have the list emailed to
you in a chronological schedule easy to print out and carry or
to leave in your Web profile.
You may also find a session road map helpful. This article lists
the Top 10 Sessions for new to Java technology developers in chronological order.
In between sessions, visit the exhibition floor. It's full of
businesses and resources who use Java technology for a profit.
Talk about good job networking opportunities! Also, many booths
hand out clever give-aways and "goodies", which in past years
have included books, pocket-sized radios, face tattoos, T-shirts,
and gummy tarantulas.
And don't forget to visit the Java Developer Connection booth.
Come by, say "Hi" and give us some feedback on how we're doing
and how we can do better.
|