Java: Classes IntroductionA class defines the abstract characteristics of a thing (object), including its attributes and what it can do. Every Java program is composed of at least one class. From a programming point of view a class can be described as a template from which direct instances are generated. In Java a class is a data structure that may contain members. A member of a class is a class element that defines an attribute or an operation. Each member can be viewed as a sub-object. When a class is declared, an optional member list declares the class members. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation. This article explains how to create a class with one member and how to instantiate it and provides a simple example of an instance being used. Simply speaking, a polygon can be described as a flat shape with three or more straight sides. The clas program defines a class representing a polygon. The definition of a polygon states that it has a number of sides, but does not specify how many sides there are. The shapes below are all polygons.
These shapes are actual objects that fall within the more generic category of a polygon. In programming terms, if the polygon is a class, each of these shapes is an instance of the polygon class. To define any of the shapes properly it is necessary to declare how many sides the shape has, and the polygon class thus needs to provide an attribute to allow this declaration to be made. This attribute is provided as a class member. The clas program declares an instance of the polygon class, called triangle, and specifies the number of its sides as three. Concepts
Source CodeThe source code listing is as follows:
Compiling and Running
Code Explanation
A class can be described as a template for objects used by the program. Classes are declared using the class keyword. The format of a simple class definition is as follows:
where:
The identifier of the class declared above is polygon. The only characteristic of the polygon class is that it contains a sides member. A member of a class is a class element that defines an attribute or an operation. A class member can exist in a number of forms including a field (or data member) which consists of data contained within the object. The format of a simple field declaration is the same as a simple variable declaration, as follows:
where:
If a program is executable one of the classes available must have a static main method that serves as the entry point. The main method is a member of the class. The clas class is included in the clas program to provide the main method, and contains one member - the main method.
The main method is the entry point and is called when the program starts. In the clas program, the main method is the only member of the clas class. Class members may be prefixed by modifiers - a list of keywords that modify the member. Other than the type specifier, the main method declaration contains two modifiers: public and static. The private, protected and public modifiers control accessibility to class members. The public modifier declares a member to be visible to everyone. The main method is declared as public because it must be visible to everyone in order to allow the program to be called. A class member that has been modified with the static keyword is accessible without requiring an instance of the containing class. What this means is that the member can be accessed without declaring any instances based on the class. All that is required is for the member to be declared, as in the example above, and it can be accessed directly. The main method is declared using the static modifier to provide a global mechanism for calling the program, since at the time of calling no instances of the clas class will have been declared.
A class constitutes a user-defined type and instances of the class may be represented by variables. When declaring the variable, the class identifier is used as the type. This statement declares a variable of type polygon named triangle.
An instance is an object based on a class. Creation of an instance from a class is called instantiation. Instantiation is the process of creating a specific object which is a member or instance of a class. It involves creating an actual instance of a declared class. In Java the new keyword is used to instantiate a class. Expression new polygon() instantiates the polygon class, i.e. it creates an instance of it. The statement above assigns the newly created instance to variable triangle. A declaration statement and instantiation statement are often combined into one, so the two statements polygon triangle ; triangle = new polygon() ; could be rewritten as polygon triangle = new polygon() ;
Once a class has been instantiated and the instance has been assigned to a variable, the instance can be accessed via the variable and so can its members. This statement assigns literal value 3 to the sides member of the triangle variable which represents an instance of the polygon class. Further CommentsMembers of Java classes can be categorised as follows:
This article discusses the field data member. Discussion of other members includes concepts that are outwith the scope of this article.
|