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 developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

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 2: Constructs for real-world applications

More-advanced Java language features

J Steven Perry, 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:  In Part 1 of this tutorial, professional Java™ programmer J. Steven Perry introduced the Java language syntax and libraries you need to write simple Java applications. Part 2, still geared toward developers new to Java application development, introduces the more-sophisticated programming constructs required for building complex, real-world Java applications. Topics covered include exception handling, inheritance and abstraction, regular expressions, generics, Java I/O, and Java serialization.

View more content in this series

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

Comments:  

Building Java applications

In this section, you will continue building up Person as a Java application. Along the way, you'll get a better idea of how an object, or collection of objects, evolves into an application.

Elements of a Java application

All Java applications need an entry point where the Java runtime knows to start executing code. That entry point is the main() method. Domain objects typically don't have main() methods, but at least one class in every application must.

You've been working since Part 1 on the example of a human-resources application that includes Person and its Employee subclasses. Now you'll see what happens when you add a new class to the application.

Creating a driver class

The purpose of a driver class (as its name implies) is to "drive" an application. Notice that this simple driver for the human-resources application contains a main() method:

package com.makotogroup.intro;
public class HumanResourcesApplication {
 public static void main(String[] args) {
 }
}

Create a driver class in Eclipse using the same procedure you used to create Person and Employee. Name the class HumanResourcesApplication, being sure to select the option to add a main() method to the class. Eclipse will generate the class for you.

Add some code to your new main() so that it looks like this:

public class HumanResourcesApplication {
. . .
  private final Logger log = Logger.getLogger(Person.class);
. . .
  public static void main(String[] args) {
    Employee e = new Employee();
    e.setName("J Smith");
    e.setEmployeeNumber("0001");
    e.setTaxpayerIdentificationNumber("123-45-6789");
    e.printAudit(log);
  }
. . .
}



Now launch the HumanResourcesApplication class and watch it run. You should see this output (with the backslashes here indicating a line continuation):

Apr 29, 2010 6:45:17 AM com.makotogroup.intro.Person printAudit
INFO: Person [age=0, eyeColor=null, gender=null, height=0, name=J Smith,\
weight=0]Employee [employeeNumber=0001, salary=null,\
taxpayerIdentificationNumber=123-45-6789]


That's really all there is to creating a simple Java application. In the next section, you'll begin looking at some of the syntax and libraries that will help you develop more-complex applications.

4 of 14 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=508383
TutorialTitle=Introduction to Java programming, Part 2: Constructs for real-world applications
publish-date=08192010
author1-email=steve@makotoconsulting.com
author1-email-cc=jaloi@us.ibm.com