Java Part 3: Primitive Types - Our new class...explained
(Page 4 of 6 )
class Number{The first line is the class definition. It tells the Java compiler that the class being defined is called “Number”. The class name must start with a capital letter. Also, the file that holds your class must have the same name as the class.
float value = 0;The next part of our class is the member declaration. This states that a “Number” object has a member called “value”, which is a float type and is set to zero every time and instance of the Number class is created.
Number(){}
Number( float val ){
value = val;
} These are called the class’s constructors. A constructor is automatically called when a new instance of a class is created. There can be multiple constructors for a class and the compiler will decide which class to call based on the type of value passed to that constructor. Constructors have no return type and if several are present, they must have different parameter types or a different numbers of arguments.
The first constructor is the default constructor; it leaves the member value
set as zero, which is the default value. The second constructor is called if an instance of the Number class is created with a float argument. Also, if an int argument is supplied to this constructor, the Java compiler will automatically cast it to a float value.
Next, we have the main method. I will discuss this method after the class methods have been discussed.
float addTo( float x ){
return value+=x;
} The addTo( float ) method seems rather simplistic and trivial, but this
is not the case. In object-orientated programming, we want to design our classes so that we never have to directly reference their member variables (such as
value) directly. Instead, we use class methods, which either modify or return the
classes members so that information is hidden from other classes and we don't have to know how every class works, but just how to use it via its member functions. This is a common practice that should be employed in all object-orientated languages.
addTo( float ), subFrom( float ), multiplyBy( float ) and divideBy( float ) are all modifier methods that allow us to change value without having to directly reference it.
The getValue() method is an accessor method. It simply returns the number stored in the value member variable.
public static void main( String[] args ) {
Number number = new Number( 5 );
number.addTo( 5 );
number.divideBy( 2 );
System.out.println( +number.getVal() );
System.exit(1);
} This is the classes main method. The first line of this method simply tells the Java compiler that the main method takes an array of Strings as an argument. We don’t use these string arguments in our example, so we will not discuss it here.
Number number = new Number( 5 );Creates a new Number object with the name number. Java variables are case sentitive, meaning that x is different from X and myVal is different from MYVAL. Because 5 was supplied as an argument to the new instance of our Number class, the second constructor is called when the object is created, and consequently, value is set to 5.0.
number.addTo(5);This line calls the addTo member function of the Number class. We could have performed the same operation with a line such as
number.value += 5;but this would have directly referenced the value member variable, and this is what we are trying to avoid.
number.divideBy( 2 );This line calls the divideBy() member function, which divides the value member by 2. The value member variable is now equal to 5 again.
System.out.println( +number.getVal() );Don’t get confused by this line just yet. It is one of Java's printing functions. The argument to println() is preceeded by a +. This tells the Java compiler to print the value which number.getVal() holds to the screen, instead of simply printing its value as a String.
The final line exits the main function and ends the application. Now that we know how to create a simple class, I will discuss compiling a java application.
Next: Compiling the Application >>
More Java Articles
More By Chris Noack