Monday 13 June 2016

Classes in Java

Classes are the building blocks of a Java application. A class can contain methods (functions), variables, initialization code, and, as we’ll discuss later, other classes. It serves as a blueprint for making class instances, which are runtime objects (individual copies) that implement the class structure. You declare a class with the class keyword. Methods and variables of the class appear inside the braces of the class declaration:

 class Pendulum {  
 float mass;  
 float length = 1.0f;  
 int cycles;  
 float getPosition ( float time ) {  
 ...  
 }  
 ...  
 }  
The Pendulum class contains three variables: mass, length, and cycles. It also defines a method called getPosition(), which takes a float value as an argument and returns a float value as a result. Variables and method declarations can appear in any order, but variable initializers can’t make “forward references” to other variables that appear later. Once we’ve defined the Pendulum class, we can create a Pendulum object (an instance of that class) as follows:
 Pendulum p;  
 p = new Pendulum();  
Recall that our declaration of the variable p doesn’t create a Pendulum object; it simply creates a variable that refers to an object of type Pendulum. We still had to create the object, using the new keyword, as shown in the second line of the preceding code snippet. Now that we’ve created a Pendulum object, we can access its variables and methods, as we’ve already seen many times:
 p.mass = 5.0;  
 float pos = p.getPosition( 1.0 );  
Two kinds of variables can be defined in a class: instance variables and static variables. Every object instance has its own set of instance variables; the values of these variables in one instance of an object can differ from the values in another object. We’ll talk about static variables later, which, in contrast, are shared among all instances of an object. In either case, if you don’t initialize a variable when you declare it, it’s given a default value appropriate for its type (null, zero, or false). The figure below shows a hypothetical TextBook application that uses two instances of Pendu lum through the reference-type variables bigPendulum and smallPendulum. Each of these Pendulum objects has its own copy of mass, length, and cycles. As with variables, methods defined in a class may be instance methods or static methods. An instance method is associated with just one instance of the class, but the relationship isn’t quite as simple as it is for variables. Instance methods are accessed through an object instance, but the object doesn’t really have its own “copy” of the methods (there is no duplication of code). Instead, the association means that instance methods can “see” and operate on the values of the instance variables of the object. As you’ll see when we talk about subclassing, there’s more to learn about how methods see variables. In that chapter, we’ll also discuss how instance methods can be “overridden” in child classes—a very important feature of object-oriented design. Both aspects differ from static methods, which we’ll see are really more like global functions, as they are associated with a class by name only.

0 comments:

Post a Comment