Application Structure and Elements

You create an application from classes. A class defines class fields to store the data, and class methods to work on the data. A class is similar to a struct in the C and C++ languages in that it can store related data of different types, but the big difference between a class and a struct is that a class also defines accessor methods to work on its data. The C and C++ languages separate functions from the struct that defines the data.

Every application needs one class with a main method. The class with the main method 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.

The ExampleProgram.java code from Introduction to the Java Platform has no fields or accessor methods. Because ExampleProgram is the only class in the program, it has a main method.

class ExampleProgram {
  public static void main(String[] args){
    System.out.println(“I am a Simple Program”);
  }
}

In the above code, the public static void keywords mean the JVM interpreter can call the program main method to start the program (public) without creating an instance of the class (static), and the program does not return data to the JVM interpreter (void) when it ends.

An instance of a class has data members and methods as defined by that class. While the class describes the data and methods to work on the data, a class instance acquires and works on the data.

The following figure shows three instances of the StoreData class by the names: FirstInstance, SecondInstance and ThirdInstance. While class instances share the same definition (class), they are separate from each other in that each instance can acquire and work on different data.

classinstances

Class Instances

It is not always necessary to create a class instance to call methods and access fields in a class. An instance of the ExampleProgram class is never created because it has no fields to access and only the one static main method to call. The main method for ExampleProgram just calls println, which is a static method in the System class. The java.lang.System class, among other things, provides functionality to send text to the terminal window where the program was started. It has all static fields and methods.

The Java platform lets a program call a method in a class without creating an instance of that class as long as the method being called is static. Just as the JVM interpreter command can call the static main method in the ExampleProgram class without creating an instance of it, the ExampleProgram class can call the static println method in the System class without creating an instance of the System class.

As you explore Java, you will come across library classes such as System, Math, or Color that contain all or some static methods and fields, and you might find that static methods and fields can make sense when you write your own classes.

For example, the Color class provides ready access to common colors such as red, blue, and magenta through its static fields, and you can get custom colors by creating a Color class instance and passing specific values to the Color class constructor.

Fields and Methods

The LessonTwoA program alters ExampleProgram to store the text string in a static field called text. The text field is static so that its data can be accessed directly by the static println method without creating an instance of the LessonTwoA class.

If you created ExampleProgram from <<>>, you can add the following examples to that project, as follows:

  1. Right-click the simple.program package
  2. Select New > Class
  3. Give the new class the name LessonTwoA
  4. Copy and paste the code below into the LessonTwoA class file and save
  5. Right-click the LessonTwoA class file and select Run As  > Java Application
  6. Check the Console window for the output

LessonTwoA class file:

package simple.program;

class LessonTwoA {
  //Static field added
  static String text = "I am a Simple Program";

  public static void main(String[] args){
    System.out.println(text);
  }
}

The LessonTwoB and LessonTwoC programs add a getText method to the program to retrieve and print the text. The LessonTwoB program accesses the non-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. The example also includes a static text field and a non-static instance method to retrieve it. A non-static method can access both static and non-static fields.

LessonTwoB class file:

Follow the instructions for creating and running LessonTwoA, except specify and use the LessonTwoB class file.

package simple.program;

class LessonTwoB {
  //Static and non-static fields
  String text = "I am a Simple Program";
  static String text2 = "I am static text";

  //Methods to access data in the fields
  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 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.

LessonTwoC class file:

Follow the instructions for creating and running LessonTwoA, except specify and use the LessonTwoC class file.

package simple.program;

class LessonTwoC {
  static String text = "I am a Simple Program";
  //Accessor method
  static String getText(){
    return text;
  }
  public static void main(String[] args){
    String retrievedText = getText();
    System.out.println(retrievedText);
  }
}

Class methods can operate only on class fields, but instance methods can operate on class and instance fields. The difference is that there is only one copy of the data stored in a class field, but each instance has its own copy of the data stored in an instance field.

For example, the following  class definition has one static field, one instance field, and two accessor methods to set the value for each field.

ExampleClass class file:

Follow the instructions for creating and running LessonTwoA, except specify and use the ExampleClass class file.

package simple.program;

class ExampleClass {
  static int FieldA = 36;
  int FieldB = 0;

  //Private methods can be called only by other methods 
  //in the same class.
  private void setFieldA (int value){
    FieldA = value;
  }
  
  private void setFieldB (int value) {
  FieldB = value;
  }
  public static void main(String[] args){
    // Do something
  }
}

If another class creates two instances of ExampleClass, then, FieldA has the value 36 and FieldB has the value 0 for both instances. The following figure shows that:

  • If another class calls setFieldA on the first instance of ExampleClass with a value of 25, then the FieldA value in both instances changes to 25.
  • If another class calls setFieldB on the first instance of ExampleClass with a value of 50, then the FieldB value in the first instance changes to 25, but the FieldB value in the other instances remains 0.
classinstance
Change class instance field values

Constructors

A constructor is a special method that prepares the new instance for use by initializing the instance fields. The constructor always has the same name as the class and no return type.

If you do not write your own constructor, the compiler adds an empty constructor. The empty constructor is called the default constructor and initializes all non-initialized fields and variables to zero. A constructor might or might not have parameters depending on whether the class provides its own initialization data or gets it from the calling method.

The following figure shows the constructor, accessor methods, and main method.

accessormethods

The LessonTwoD program converts the LessonTwoB program to use a constructor without parameters to initialize the text string field.

LessonTwoD class file:

Follow the instructions for creating and running LessonTwoA, except specify and use the LessonTwoD class file.

package simple.program;

class LessonTwoD{
  String text;

  //Constructor
   LessonTwoD(){
     text = "I am a Simple Program";
   }
   String getText(){
     return text;
   }

   public static void main(String[] args){
     LessonTwoD progInst = new LessonTwoD();
     String retrievedText = progInst.getText();
     System.out.println(retrievedText);
   }
 }

The LessonTwoE program passes the string to be printed to the constructor as a parameter:

LessonTwoE class file:

Follow the instructions for creating and running LessonTwoA, except specify and use the LessonTwoE class file.

package simple.program;

class LessonTwoE{
  String text;

  //Constructor
  LessonTwoE(String message){
    text = message;
  }
  String getText(){
    return text;
  }
  public static void main(String[] args){
    LessonTwoE progInst = new 
       LessonTwoE("I am a simple program");
    String retrievedText = progInst.getText();
    System.out.println(retrievedText);
  }
}
Application Structure and Elements

Leave a comment