|
 |
|
 |
» Java Class Basics: Static Methods by bdl |
|
(Login to remove green text ads)
A quick rundown of what static methods and fields are, and how to implement them in your class designs.
Assumes a basic knowledge of OOP and Java.
What makes a class field (class variable, hereon out referred to as a field) or method 'static'? The fact that it exists once in any group of objects; that is, if you have 10 instances of the same object, that data or method will be shared by all 10. The instantiated objects will have their own versions of other data and methods, but only a single copy of the ones defined as static. That's very useful because you can retrieve the data or use the method virtually anywhere without having to actually create an object first.
To start, let's take a look at one of the most basic components of Java, the System.out.println() method. Anyone who's ever read a Java tutorial or even skimmed the surface of some basic code has likely run across this method. It's provided by the 'System' class in the java.lang package, and simply stated, outputs some text or other data to the command line, followed by a newline. An example:
Code:
class StartStatic
{
public static void main(String[] args)
{
System.out.println("This is a static method!");
}
}
Very simple statement that outputs the text 'This is a static method!' along with a newline, and then exits. Notice how you don't have to create an instance of the System class, you can just call the method on its own. This is one of the factors that makes it static, i.e. it can be called from any other class on a whim without having to be instantiated. You've no doubt notice the other 'static' in the example above, the definition for the main method. It defines that method as being used only once in the class, i.e. to start the Java application.
So far I've only touched on using static methods; what about data fields defined as 'static'? A perfect example is the Math.PI constant. It's defined once as static in the 'Math' class, and although it can be accessed from any other class, it can't be changed, and only one copy of it exists anywhere. All objects that use the Math class have access to that one static value.
Let's take a look at a practical use of static fields and methods, in an example class called 'Feet2Meters'. The purpose of the class is to change feet to meters, and return that value to whatever class requires a conversion like that.
Code:
public class Feet2Meters
{
// Define a constant with the conversion factor of feet to meters
public static final double feet2meters = 0.3048;
// Define a static method that converts feet to meters
public static double convert(double feet)
{
return feet * feet2meters;
}
// Define a main method to test the class
public static void main(String[] args)
{
// initialize a variable called 'feet'
double feet = 100;
/*
initialize a variable called 'meters' that holds the
returning value of the convert() method
*/
double meters = Feet2Meters.convert(feet);
// output
System.out.println(feet +" feet is "+ meters +" meters.");
}
}
Ok let's take it step by step. After declaring the class, we create a constant called 'feet2meters' that will hold a non-changing value to calculate the ratio of feet to meters. Just like PI, this value should never change, unless perhaps the creator of the class should want to take it out another decimal place or two for precision's sake. The convention 0.3048 is sufficient for what we want to do here, I think. The great thing about defining this as static, it can be changed once and have immediate effects across any class that uses this field. Normally a static class field would be a constant; you might run across a use for a static variable, but it's probably not as likely.
Next we define a single method to perform the conversion, 'convert()'. As stated, it is public, static, and returns a double. When called from any method as Feet2Meters.convert(value_of_feet), it will return a double value with the sum of whatever value of feet you pass to it and our static field 'feet2meters'. Notice the way you call the method, with the 'dot' notation class.method(). That way you can call the method from any other class without having to instantiate an object first; use the full classname, a 'dot' and the method name.
The main() method that follows that is a quick test and example of how to implement the convert() method. Once you compile the Feet2Meters.java file into bytecode, you can execute it with 'java Feet2Meters' and get the following output:
Code:
100.0 feet is 30.48 meters.
Note you can always instantiate an object of the Feet2Meters class first, then call the method, e.g.
Code:
class Feet2MetersTest
{
public static void main(String[] args)
{
// Initialize a value for 'feet'
double feet = 1000;
// Instantiate a new Feet2Meters object
Feet2Meters f2m = new Feet2Meters();
// The value of 'meters' receives the convert() method's output
double meters = f2m.convert(feet);
System.out.println("1000 feet is "+ meters +" meters.");
}
}
By now you should have a pretty good idea of what a static method is, and how to implement them in your code. The classes shown are working examples, feel free to use them as you wish.
References:
Core Java 2 Vol. 1 - Horstmann / Cornell - Sun Microsystems Press, 2003
LINK: <java.sun.com API documentation>
|
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|