Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Introduction to Java programming, Part 1: Java language basics

Object-oriented programming on the Java platform

J Steven Perry (steve.perry@makotoconsulting.com), Principal Consultant, Makoto Consulting Group, Inc.
Photo of J Steven Perry
J. Steven Perry is a software developer, architect, and general Java nut who has been developing software professionally since 1991. His professional interests range from the inner workings of the JVM to UML modeling and everything in between. Steve has a passion for writing and mentoring; he is the author of Java Management Extensions (O'Reilly), Log4j (O'Reilly), and the IBM developerWorks articles "Joda-Time" and OpenID for Java Web applications." In his spare time, he hangs out with his three kids, rides his bike, and teaches yoga.

Summary:  This two-part tutorial introduces the structure, syntax, and programming paradigm of the Java™ language and platform. You'll learn the Java syntax you are most likely to encounter professionally and Java programming idioms you can use to build robust, maintainable Java applications. In Part 1, J. Steven Perry guides you through the essentials of object-oriented programming on the Java platform, including fundamental Java syntax and its use. You'll get started with creating Java objects and adding behavior to them, and conclude with an introduction to the Java Collections Framework, with considerable ground covered in between.

View more content in this series

Date:  19 Aug 2010
Level:  Introductory PDF:  A4 and Letter (1290 KB | 57 pages)Get Adobe® Reader®

Activity:  620445 views
Comments:  

Adding behavior to a Java object

Person is looking good so far, but it could use some additional behavior to make it more interesting. As you've already learned, creating behavior means adding methods. This section looks more closely at accessor methods — namely, the getters and setters you've already seen in action. You'll also learn syntax for calling methods.

Accessor methods

In order to encapsulate a class's data from other objects, you declare its variables to be private and then provide accessor methods. As you know, a getter is an accessor method for retrieving the value of an attribute; a setter is an accessor method for modifying that value. The naming of accessors follows a strict convention known as the JavaBeans pattern, whereby any attribute Foo has a getter called getFoo() and a setter called setFoo().

The JavaBeans pattern is so common that support for it is built right into the Eclipse IDE. You've even already seen it in action — when you generated getters and setters for Person in the preceding section.

Accessors follow these guidelines:

  • The attribute itself is always declared with private access.
  • The access specifier for getters and setters is public.
  • Getters don't take any parameters and return a value whose type is the same as the attribute it accesses.
  • Settings only take one parameter, of the type of the attribute, and do not return a value.

Declaring accessors

By far the easiest way to declare accessors is to let Eclipse do it for you, as shown back in Figure 6. But you should also know how to hand-code a getter-and-setter pair. Suppose you have an attribute, Foo, whose type is java.lang.String. A complete declaration for it (following the accessor guidelines) would be:

private String foo;
public String getFoo() {
  return foo;
}
public void setFoo(String value) {
  foo = value;
}

You may notice right away that the parameter value passed to the setter is named differently than if it had been generated by Eclipse. This is my own convention, and one I recommend to other developers. On the rare occasion that I do hand-code a setter, I always use the name value as the parameter value to the setter. This eye-catcher reminds that I've hand-coded the setter. Because I usually allow Eclipse to generate getters and setters for me, when I don't there's a good reason for it. Using value as the setter's parameter value reminds me that this setter is special. (Code comments could also do that.)


Calling methods

Invoking, or calling, methods is easy. You saw in Listing 7 how to invoke the various getters of Person to return their values. Now I'll formalize the mechanics of making method calls.

Method invocation with and without parameters

To invoke a method on an object, you need a reference to that object. Method-invocation syntax comprises the object reference, a literal dot, the method name, and any parameters that need to be passed:

objectReference.someMethod();
objectReference.someOtherMethod(parameter);

Here is a method invocation without parameters:

Person p = /*obtain somehow */;
p.getName();

And here is a method invocation with parameters (accessing the Name attribute of Person):

Person p = new Person("Joe Q Author", 42, 173, 82, "Brown", "MALE");

Remember that constructors are methods, too. And you can separate the parameters with spaces and newlines. The Java compiler doesn't care. These next two method invocations are identical:

new Person("Joe Q Author", 42, 173, 82, "Brown", "MALE");

new Person("Joe Q Author", 
        42, 
        173, 
        82, 
        "Brown", 
        "MALE");

Nested method invocation

Method invocations can also be nested:

Logger l = Logger.getLogger(Person.class.getName());
l.info("Name: " + p.getName());

Here you are passing the return value of Person.class.getName() to the getLogger() method. Remember that the getLogger() method call is a static method call, so its syntax differs slightly. (You don't need a Logger reference to make the invocation; instead, you just use the name of the class itself as the left-hand side of the invocation.)

That's really all there is to method invocation.

9 of 18 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=507256
TutorialTitle=Introduction to Java programming, Part 1: Java language basics
publish-date=08192010
author1-email=steve.perry@makotoconsulting.com
author1-email-cc=jaloi@us.ibm.com