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.
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:
> 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
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);
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.