Classes

Team-Fly

In terms of OOP, a class is a grouping of like objects, and each object within a class is called an instance of that class. A simple programming example of the class concept is a class called Button; you may have several buttons, such as Save, Close, Open, and Cancel, in an application. Each button belongs to the Button class, yet each one can contain different variable values, such as the label or shape. Note that although the variable of each button object can contain different values, all buttons have the same variable declaration and also have the same methods as the Button class. Object variables are often called instance variables.

To reinforce the idea of objects and classes, we will build a simple Java program that defines a class with a few variables and a method and then declares two objects from this class.

Note 

The purpose of the code example is to demonstrate the class-to-object relationship, not to go through the details of Java syntax. The basics of Java syntax are covered in Chapters 7 through 10 in this book.

public class jsap0601 { /* Declare some instance variables.          */     String name;     int age; /* Declare a method for the class         */     void out()     {         System.out.println("\n " + name + " is " + age + " years old.");     } /* Main declaration, required for application (instead of applet) */     public static void main (String[] args)     {         System.out.println("Demonstration of Classes and Objects"); /* Declare 1st instance of class JSAP0601 */         jsap0601 person1 = new jsap0601();/* Declare 2nd instance of class JSAP0601 */         jsap0601 person2 = new jsap0601(); /* Assign values to both instances */         person1.name = "Ken";         person1.age  = 36;         person2.name = "Donna";         person2.age  = 35; /* Call method from class jsap0601 for each instance */         person1.out();         person2.out();     } }

The output of this program follows.

Ken is 36 years old. Donna is 35 years old.

To make everything a bit more complicated, you can declare variables and methods that belong to the class itself. These variables and methods can be accessed directly or from an instance of the class; however, class methods cannot access instance variables or methods. This relationship is shown in Figure 6.2. Class methods must access class variables.

click to expand

Figure 6.2: Variable and method relationships between class and object

An example of a class variable is the total number of instance objects created. Then, as objects are added or deleted from the class, the accumulator can be adjusted accordingly. The following code example demonstrates the use of both class variables and methods.

public class jsap0602 { /* Declare a Class Variable                  */     static int total; /* Declare some instance variables.          */     String name;     int age; /* Declare a Class method for the class      */     static void add()     {         total = total + 1;     }     static void total_out()     {         System.out.println("\nTotal number of people = " + total);     } /* Declare an instance method for the class         */     void out()     {         System.out.println("\n" + name + " is " + age + " years old.");     } /* Main declaration, required for application (instead of applet) */     public static void main (String[] args)     {         System.out.println("Demonstration of Classes and Objects"); /* Declare 1st instance of class JSAP0602 */         jsap0602 person1 = new jsap0602(); /* Declare 2nd instance of class JSAP0602 */         jsap0602 person2 = new jsap0602();/* Assign values to both instances and add to class variable */         person1.name = "Ken";         person1.age  = 36;         jsap0602.add();         person2.name = "Donna";         person2.age  = 35;         jsap0602.add(); /* Call method from class jsap0602 for each instance */         person1.out();         person2.out(); /* Write out the total class variable which is totaling people */         jsap0602.total_out();     } }

The preceding code has the following output:

Ken is 36 years old. Donna is 35 years old. Total number of people = 2

When designing classes, keep the following elements in mind.

  • The class should have a well-defined interface, or way that other programs can access it.

  • If possible, a class should not have to rely on another class to perform the intended function.

  • The class should be bulletproof, so that any input, bad or good, will have a predictable response.

  • The purpose of the class must be well defined. Hence the class should have an easy-to-understand function or solve a specific problem in the application.

Objects help programmers by providing modularity and the ability to encapsulate data. Classes provide the benefit of reusability because the code is written once and then objects can be declared multiple times and reuse the same code.


Team-Fly


Java & BAPI Technology for SAP
Java & BAPI Technology for SAP
ISBN: 761523057
EAN: N/A
Year: 1998
Pages: 199

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net