Communities
|
Social Applications
Networks
Support
|
|
C-Level Executives
Other Roles
|
|
Support
Education
Partner
Other Tasks
|
|
[ <<BACK] [ CONTENTS] [ NEXT>>]
All programs written in the Java language (Java programs) are built from classes. Because all classes have the same structure and share common elements, all Java programs are very similar.
This lesson describes the structure and elements of a simple application created from one class. The next lesson covers the same material for applets.
| |
Application Structure and Elements
An application is created from classes. A
class is similar to a
RECORD in the Pascal language or a
struct in the C language in that it stores related data in
fields, where the fields can be different types. So you could, for example, store a text string in one field, an integer in another field, and a floating point in a third field. The difference between a class and a
RECORD or
struct is that a class also defines the
methods to work on the data.
For example, a very simple class might store a string of text and define one method to set the string and another method to get the string and print it to the console. Methods that work on the data are called accessor methods.
Every application needs one class with a
main method. This class is the entry point for the program, and is the class name passed to the
java interpreter command to run the application.
The code in the
main method executes first when the program starts, and is the control point from which the controller class accessor methods are called to work on the data.
Here, again, is the
example program from Lesson 1. It has no fields or accessor methods, but because it is the only class in the program, it has a
main method.
class ExampleProgram {
public static void main(String[] args){
System.out.println("I'm a Simple Program");
}
}
|
The
public static void keywords mean the Java
1
virtual machine (JVM) interpreter can call the program's
main method to start the program (public) without creating an instance of the class (static), and the program does not return data to the Java VM interpreter (void) when it ends.
An instance of a class is an executable copy of the class While the class describes the data and behavior, you need a class instance to acquire and work on data. The diagram at the left shows three instances of the
ExampleProgram class by the names:
FirstInstance,
SecondInstance and
ThirdInstance.
The
main method is static to give the Java VM interpreter a way to start the class without creating an instance of the control class first. Instances of the control class are created in the
main method after the program starts.
The
main method for the simple example does not create an instance of the
ExampleProgram class because none is needed. The
ExampleProgram class has no other methods or fields, so no class instance is needed to access them from the
main method. The Java platform lets you execute a class without creating an instance of that class as long as its static methods do not call any non-static methods or fields.
The
ExampleProgram class just calls
System.out.println. The
java.lang.System class, among other things, has a static
out field of type
PrintStream that is used to invoke the
print(ln) methods in the
PrintStream class.
The static fields and methods of a class can be called by another program without creating an instance of the class. So, just as the Java VM interpreter command could call the
static main method in the
ExampleProgram class without creating an instance of the
ExampleProgram class, the
ExampleProgram class can call the
static println method in the
System class, without creating an instance of the
System class.
However, a program must create an instance of a class to access its non-static fields and methods. Accessing static and non-static fields and methods is discussed further with several examples in the next section.
Fields and Methods
The
LessonTwoA.java program alters the simple example to store the text string in a static field called
text. The
text field is static so its data can be accessed directly by the static call to
out.println without creating an
instance of the
LessonTwoA class.
class LessonTwoA {
static String text = "I'm a Simple Program";
public static void main(String[] args){
System.out.println(text);
}
}
|
The
LessonTwoB.java and
LessonTwoC.java programs add a
getText method to the program to retrieve and print the text.
The
LessonTwoB.java program accesses the static
text field with the non-static
getText method. Non-static methods and fields are called instance methods and fields. This approach requires that an instance of the
LessonTwoB class be created in the
main method. To keep things interesting, this example includes a static text field and a non-static instance method (
getStaticText) to retrieve it.
Note: The field and method return values are all type
![]()
String.
![]()
class LessonTwoB {
String text = "I'm a Simple Program";
static String text2 = "I'm static text";
String getText(){
return text;
}
String getStaticText(){
return text2;
}
public static void main(String[] args){
LessonTwoB progInstance = new LessonTwoB();
String retrievedText = progInstance.getText();
String retrievedStaticText =
progInstance.getStaticText();
System.out.println(retrievedText);
System.out.println(retrievedStaticText);
}
}
|
The
LessonTwoC.java program accesses the static
text field with the static
getText method. Static methods and fields are called class methods and fields. This approach allows the program to call the static
getText method directly without creating an instance of the
LessonTwoC class.
class LessonTwoC {
static String text = "I'm a Simple Program";
//Accessor method
static String getText(){
return text;
}
public static void main(String[] args){
String retrievedText = getText();
System.out.println(retrievedText);
}
}
|
So, class methods can operate only on class fields, and instance methods can operate on class and instance fields.
You might wonder what the difference means. In short, there is only one copy of the data stored or set in a class field but each instance has its own copy of the data stored or set in an instance field.
The figure above shows three class instances with one static field and one instance field. At runtime, there is one copy of the value for static Field A and each instance points to the one copy. When setFieldA(50) is called on the first instance, the value of the one copy changes from 36 to 50 and all three instances point to the new value. But, when setFieldB(25) is called on the first instance, the value for Field B changes from 0 to 25 for the first instance only because each instance has its own copy of Field B.
See Understanding Instance and Class Members lesson in The Java tutorial for a thorough discussion of this topic.
Constructors
Classes have a special method called a
constructor that is called when a class instance is created. The class constructor always has the same name as the class and no return type. The
LessonTwoD program converts the
LessonTwoB program to use a constructor to initialize the text string.
Note: If you do not write your own constructor, the compiler adds an empty constructor, which calls the no-arguments constructor of its parent class. The empty constructor is called the default constructor. The default constructor initializes all non-initialized fields and variables to zero.
![]()
![]()
class LessonTwoD {
String text;
//Constructor
LessonTwoD(){
text = "I'm a Simple Program";
}
//Accessor method
String getText(){
return text;
}
public static void main(String[] args){
LessonTwoD progInst = new LessonTwoD();
String retrievedText = progInst.getText();
System.out.println(retrievedText);
}
}
|
To Summarize
A simple program that prints a short text string to the console would probably do everything in the
main method and do away with the constructor,
text field, and
getText method. But, this lesson used a very simple program to show you the structure and elements in a basic Java program.
More Information
See Understanding Instance and Class Members lesson in The Java tutorial for a thorough discussion of this topic.
_______
1
As used on this web site, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform.
[ TOP]

