Section 3.3. Class and Object Methods

[Page 41 (continued)]

3.3. Class and Object Methods

Java also understands about functions. Remember functions from algebra? They're a "machine or box" into which you put one value, and out comes another. Java calls these methods.

However, you can't just call a function or method in Java like you can in some other languages. Every method or function in Java must be defined inside a class. There are two types of methods in Java: class methods or object methods. Class methods are methods that can be executed using the class name or on an object of the class. Object methods can only be executed on an object of the class. Class methods are used for general methods that don't pertain to a particular object. They are defined using the keyword static. Object methods work with a particular object's data (the object the method was called on).

3.3.1. Invoking Class Methods

Class methods can be invoked (executed) by using the class name followed by a period and then the method name: ClassName.methodName();. By convention, class names in Java start with an uppercase letter: like Character. The Character class is a wrapper class for the primitive type char. It also provides general character methods.

Making it Work Tip: Wrapper Classes

Wrapper classes are classes that you use to "wrap'' around primitive types in order to have objects to work with. Many general purpose classes in Java, such as the collection classes (List and Set), require the values that you add to the collections to be objects. Since primitive types are not objects you wouldn't be able to use them in collections (prior to Java version 5.0). However, if you wrap a primitive type with a wrapper object you will be able to use it with classes that require objects. As of Java version 5.0 (also called jdk 1.5) the wrapping of a primitive value is automatically done when it is needed. This is called boxing and unboxing.

One of the class methods for the Character class takes a character as the input value (the value that goes into the box) and returns (the value that comes out of the box) the number that is the integer value for that character. Characters in Java are specified between single quotes: 'A'. The name of that method is getNumeric-Value and you can use System.out.println to display the value that the method getNumericValue returns:

[Page 42]

> System.out.println(Character.getNumericValue('A')); 10

Another class method that's built in to the Math class in Java is named absit's the absolute value function. It returns the absolute value of the input numeric value.

> System.out.println(Math.abs(1)); 1 > System.out.println(Math.abs(-1)); 1

Debugging Tip: Common Typos

If you type a class name and Java can't figure out what class you are talking about, you will get an undefined class error.

> Mat.abs(-3); Error: Undefined class 'Mat'

If you mistype a method (function) name, you will get the following error:

> Math.ab(-3); Error: No 'ab' method in 'java.lang.Math'

3.3.2. Executing Object Methods

Object methods are methods that must be executed on an object using:


An object reference can be the name of an object variable. You can't invoke object methods using the class name like you can with class methods.

In Java there is a String class, which is how you represent lists of characters (letters), like the letters of a person's name. Objects of the String class are created by the compiler whenever it sees string literals (characters enclosed with double quotes), like "Barbara" or "cat.jpg". The double quotes tell the compiler that this is an object of the String class and not a variable name.

There are many object methods in the String class, such as toLowerCase() and toUpperCase(). These methods actually create and return new String objects (objects of the class String). See the API (application program interface) for the String class for a full listing of the available methods.

> String name = "Fred Farmer"; > System.out.println(name); Fred Farmer > String lowerName = name.toLowerCase(); > System.out.println(lowerName); 
[Page 43]
fred farmer > String upperName = name.toUpperCase(); > System.out.println(upperName); FRED FARMER > System.out.println(name); Fred Farmer

Notice that the value of name didn't change even though we invoked the method toLowerCase on it. All of the String methods that can modify a string don't change the original string but instead return a new string with the action done on that string. We say that strings are immutable, meaning that they don't change.

Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Year: 2007
Pages: 191 © 2008-2017.
If you may any questions please contact us: