Section 7.6. static Methods

   

7.6 static Methods

static methods are methods that do not operate on objects. You call them on a class. They do not need objects to carry out their tasks and therefore have no implicit parameter ”there is no this . Our square class in Chapter 4 defined a couple of static methods:

 public static int square(int x) {     return x * x; }     public static void show(int ans) {        System.out.println("The answer is " + ans); } 

As you can see, there is no object required to carry out this operation, so none is created. All of the methods in the java.lang.Math class are static methods. When you think about it, it makes a lot of sense: they just perform generic operations, such as calculating the result of one value to the power of another or the smaller of two passed values. The following example makes a call to Math.random() , a famous static class:

7.6.1 MakeRandom.java

 package chp7;  public class MakeRandom {     public static void main(String[] args) {         MakeRandom sn = new MakeRandom();         System.out.println("The number is: " + sn.getNumber());     }     public double getNumber() {             // Math.random is a static method         return Math.random();     } } 

Because static methods don't work with objects, they also don't allow you access to instance fields. There is no instance. Say you've got a class called Human. Allowing static methods access to instance fields would be like asking a static method to tell you the hair color of Human. Not going to happen.

On the other hand, static methods do have access to static fields in their class. In order to get the static field value, you need to call the method on the name of the class. The following overblown example shows how this works:

7.6.2 FieldTest.java

 package chp7;  public class FieldTest {     static int num;     static int makeNumber() {         int r = num;         num++;         return r;     }     static int id = makeNumber();     public static void main(String[] args) {         System.out.println(FieldTest.id);     } } 

You cannot call non- static methods from inside static methods, although you can do the opposite . You will find people on occasion who claim that static methods are not object oriented because you're not sending a message to an object to do your work. That may be the case, but they're certainly used to good effect in the API, which is good enough for me.

7.6.3 factory Methods

Classes usually provide a public constructor for the creation of objects. Classes can also provide a public static factory method. A static factory method returns an instance of the class, and it can be used in addition to or instead of constructors. For example, the boolean wrapper class defines a static factory method that takes a primitive boolean value and transforms it into an object reference. That static method looks like this:

 public static Boolean valueOf(boolean b) {    return (b ? Boolean.TRUE : Boolean.FALSE); } 

Note

Here the Immediate If is used to determine the field to return. The Immediate If is used the same way in ColdFusion, but it has a different syntax altogether: Its structure in ColdFusion is like this: #IIF( Hour(Now()) GT 12, DE("It is after noon"), DE("It is morning"))# .


It can be advantageous to use static factory methods for a couple of reasons. For one, they can save space because they don't have to create a new object every time they are invoked. That means you won't have unnecessary objects causing confusion and degrading performance. Also, they are not limited in the same way constructors are. A class can only create one constructor with a given signature. If it seems like a good idea to write more than one constructor with the same signature, then you might do well to offload the work into static factory methods whose names indicate the distinct nature of their usefulness .


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net