Java Reference
In-Depth Information
Chapter 2
Java on a PC
In this chapter, we will expand on your knowledge of Java applications by showing how to instantiate (create) an
object and demonstrating how constructors work. We will highlight the key differences between running a Java class
as an application and creating an object. We will also provide a more detailed explanation of how Java “works” and
“what comes with” Java. This explanation will also cover what really happened when you clicked buttons and chose
RAD (Rational Application Developer) options in the previous chapter. In addition, we will explore some online
documentation that explains the classes that “come with” Java.
After completing the chapter, you should understand the following:
Instantiation and objects
The SDK, JVM, and JRE
Bytecode
.java and .class files
Compiling Using WebSphere, you will be able to:
Create Java classes that pass and receive parameters
Java Applications
In the previous chapter, the Java class EmployeeApp was created to be run as a Java application (i.e., EmployeeApp
had a main method). In the real world, very few Java classes are run as applications. Most Java classes are instantiated.
In other words, an instance of the class (called an object) is created. When an object is created the class variables are
created and the constructor method is run. For example, if there was a class called Employee and an Employee object
was created, the Employee class variables would be created and the Employee constructor would be run.
Constructors are primarily used to perform “set up” functions. This may mean initializing variables, creating
objects, setting property values, establishing connections to remote resources, etc. Initially the constructor
examples will seem very simple but as we expand the examples in future chapters, the real power and purpose of the
constructor will become apparent.
In the previous chapter's example, we created a single Java class. In this chapter, we will create a second Java
class called Employee that will be instantiated. Employee's purpose will be to display Employee information. We will
modify EmployeeApp to pass an Employee name to Employee and then execute the Employee display method.
 
Search WWH ::




Custom Search