Certification Objective Identifiers JavaBeans (Objectives 1.3 and 1.4)


Certification Objective —Identifiers & JavaBeans (Objectives 1.3 and 1.4)

1.3 Develop code that declares, initializes, and uses primitives, arrays, enums, and objects as static, instance, and local variables. Also, use legal identifiers for variable names.

1.4 Develop code that declares both static and non-static methods, andif appropriate— use method names that adhere to the JavaBeans naming standards. Also develop code that declares and uses a variable-length argument list.

Remember that when we list one or more Certification Objectives in the book, as we just, did, it means that the following section covers at least some part of that objective. Some objectives will be covered in several different chapters, so you'll see the same objective in more than one place in the book. For example, this section covers declarations, identifiers, and JavaBeans naming, but using the things you declare is covered primarily in later chapters.

So, we'll start with Java identifiers. The three aspects of Java identifiers that we cover here are

  • Legal Identifiers The rules the compiler uses to determine whether a name is legal.

  • Sun's Java Code Conventions Sun's recommendations for naming classes, variables, and methods. We typically adhere to these standards throughout the book, except when we're trying to show you how a tricky exam question might be coded. You won't be asked questions about the Java Code Conventions, but we strongly recommend that programmers use them.

  • JavaBeans Naming Standards The naming requirements of the JavaBeans specification. You don't need to study the JavaBeans spec for the exam, but you do need to know a few basic JavaBeans naming rules we cover in this chapter.

Legal Identifiers

Technically, legal identifiers must be composed of only Unicode characters, numbers, currency symbols, and connecting characters (like underscores). The exam doesn't dive into the details of which ranges of the Unicode character set are considered to qualify as letters and digits. So, for example, you won't need to know that Tibetan digits range from \u0420 to \uOf29. Here are the rules you do need to know:

  • Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number!

  • After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers.

  • In practice, there is no limit to the number of characters an identifier can contain.

  • You can't use a Java keyword as an identifier. Table 1-1 lists all of the Java keywords including one new one for 5.0, enum.

  • Identifiers in Java are case-sensitive;foo and Foo are two different identifiers.

Table 1-1: Complete List of Java Keywords (assert added in 1.4, enum added in 1.5)

abstract

boolean

break

byte

case

catch

char

class

const

continue

default

do

double

else

extends

final

finally

float

for

goto

if

implements

import

instanceof

int

interface

long

native

new

package

private

protected

public

return

short

static

strictfp

super

switch

synchronized

this

throw

throws

transient

try

void

volatile

while

assert

enum

    

Examples of legal and illegal identifiers follow, first some legal identifiers:

 int _a; int $c; int ______2_w; int _$; int this_is_a_very_detailed_name_for_an_identifier; 

The following are illegal (it's your job to recognize why):

 int :b; int -d; int e#; int .f; int 7g; 

Sun's Java Code Conventions

Sun estimates that over the lifetime of a standard piece of code, 20 percent of the effort will go into the original creation and testing of the code, and 80 percent of the effort will go into the subsequent maintenance and enhancement of the code. Agreeing on, and coding to, a set of code standards helps to reduce the effort involved in testing, maintaining, and enhancing any piece of code. Sun has created a set of coding standards for Java, and published those standards in a document cleverly titled "Java Code Conventions," which you can find at java.sun.com. It's a great document, short and easy to read and we recommend it highly.

That said, you'll find that many of the questions in the exam don't follow the code conventions, because of the limitations in the test engine that is used to deliver the exam internationally. One of the great things about the Sun certifications is that the exams are administered uniformly throughout the world. In order to achieve that, the code listings that you'll see in the real exam are often quite cramped, and do not follow Sun's code standards. In order to toughen you up for the exam, we'll often present code listings that have a similarly cramped look and feel, often indenting our code only two spaces as opposed to the Sun standard of four.

We'll also jam our curly braces together unnaturally, and sometimes put several statements on the same lineouch! For example:

  1. class Wombat implements Runnable {  2.   private int i;  3.   public synchronized void run() {  4.     if (i%5 != 0) { i++; }  5.     for(int x=0; x<5; x++, i++)  6.       { if (x > 1) Thread.yield(); }  7.     System.out.print(i + " ");  8.   }  9.   public static void main(String[] args) { 10.     Wombat n = new Wombat(); 11.     for(int x=100; x>0; --x) { new Thread(n).start(); } 12. } } 

Consider yourself forewarned—you'll see lots of code listings, mock questions, and real exam questions that are this sick and twisted. Nobody wants you to write your code like this. Not your employer, not your coworkers, not us, not Sun, and not the exam creation team! Code like this was created only so that complex concepts could be tested within a universal testing tool. The one standard that is followed as much as possible in the real exam are the naming standards. Here are the naming standards that Sun recommends, and that we use in the exam and in most of the book:

  • Classes and interfaces The first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be uppercase (a format that's sometimes called "camelCase"). For classes, the names should typically be nouns. For example:

     Dog Account PrintWriter 

    For interfaces, the names should typically be adjectives like

     Runnable Serializable 

  • Methods The first letter should be lowercase, and then normal camelCase rules should be used. In addition, the names should typically be verb-noun pairs. For example:

     getBalance doCalculation setCustomerName 

  • Variables Like methods, the camelCase format should be used, starting with a lowercase letter. Sun recommends short, meaningful names, which sounds good to us. Some examples:

     buttonWidth accountBalance myString 

  • Constants Java constants are created by marking variables static and final. They should be named using uppercase letters with underscore characters as separators:

     MIN__HEIGHT 

JavaBeans Standards

The JavaBeans spec is intended to help Java developers create Java components that can be easily used by other Java developers in a visual Integrated Development Environment (IDE) tool (like Eclipse or NetBeans). As a Java programmer, you want to be able to use components from the Java API, but it would be great if you could also buy the Java component you want from "Beans 'R Us," that software company down the street. And once you've found the components, you'd like to be able to access them through a development tool in such a way that you don't have to write all your code from scratch. By using naming rules, the JavaBeans spec helps guarantee that tools can recognize and use components built by different developers. The JavaBeans API is quite involved, but you'll need to study only a few basics for the exam.

First, JavaBeans are Java classes that have properties. For our purposes, think of properties as private instance variables. Since they're private, the only way they can be accessed from outside of their class is through methods in the class. The methods that change a property's value are called setter methods, and the methods that retrieve a property's value are called getter methods. The JavaBean naming rules that you'll need to know for the exam are the following:

JavaBean Property Naming Rules

  • If the property is not a boolean, the getter method's prefix must be get. For example, getSize() is a valid JavaBeans getter name for a property named "size." Keep in mind that you do not need to have a variable named size (although some IDEs expect it). The name of the property is inferred from the getters and setters, not through any variables in your class. What you return from getsize() is up to you.

  • If the property is a boolean, the getter method's prefix is either get or is. For example, getStopped() or isstopped() are both valid JavaBeans names for a boolean property.

  • The setter method's prefix must be set. For example, setsize() is the valid JavaBean name for a property named size.

  • To complete the name of a getter or setter method, change the first letter of the property name to uppercase, and then append it to the appropriate prefix (get, is, or set).

  • Setter method signatures must be marked public, with a void return type and an argument that represents the property type.

  • Getter method signatures must be marked public, take no arguments, and have a return type that matches the argument type of the setter method for that property.

Second, the JavaBean spec supports events, which allow components to notify each other when something happens. The event model is often used in GUI applications when an event like a mouse click is multicast to many other objects that may have things to do when the mouse click occurs. The objects that receive the information that an event occurred are called listeners. For the exam, you need to know that the methods that are used to add or remove listeners from an event must also follow JavaBean naming standards:

JavaBean Listener Naming Rules

  • Listener method names used to "register" a listener with an event source must use the prefix add, followed by the listener type. For example, addActionzistener() is a valid name for a method that an event source will have to allow others to register for Action events.

  • Listener method names used to remove ("unregister") a listener must use the prefix remove, followed by the listener type (using the same rules as the registration add method).

  • The type of listener to be added or removed must be passed as the argument to the method.

Examples of valid JavaBean method signatures are

 public void setMyValue(int v) public int getMyvalue() public boolean isMyStatus() public void addMyListener(MyListener m) public void removeMyListener(MyListener m) 

Examples of invalid JavaBean method signatures are

 void setCustomerName(String s)         // must be public public void modifyMyValue(int v)       // can't use 'modify' public void addXListener(MyListener m) // listener type mismatch 

image from book
Exam Watch

The objective says you have to know legal identifiers only for variable names, but the rules are the same for ALL Java components. So remember that a legal identifier for a variable is also a legal identifier for a method or a class. However, you need to distinguish between legal identifiers and naming conventions, such as the JavaBeans standards, that indicate how a Java component should be named. In other words, you must be able to recognize that an identifier is legal even if it doesn't conform to naming standards. If the exam question is asking about naming conventions—not just whether an identifier will compile—JavaBeans will be mentioned explicitly.

image from book



SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net