JAVA
STATIC
TUTORIAL
STATIC
TUTORIAL
CONTENTS
• I. INTRODUCTION
• II. STATIC METHOD
• III. STATIC BINDING
• IV. STATIC IMPORT
• V. REFERENCES
I. INTRODUCTION
Hello; nice to meet you. Welcome to the “Java Static Tutorial.”
This tutorial has been prepared using Microsoft Windows, NetBeans IDE 7.0.1 and JDK 1.7.
If you find any errors or better ways of presenting the information please post them as tutorial comments.
II. STATIC METHOD
A static method is a method that performs a simple common task without creating an object. Arguments in the static method call supply the static method with any data required to complete its task. The “this” keyword can not be used in a static method.
A. STATIC METHOD DECLARATION
Placing the keyword static before the return type in the method’s declaration declares the method static. For example:
public static void main(String[] args){
} // end main
An instance of a class (an object) is not created when the static main method is called.
B. STATIC METHOD SYNTAX
The syntax for calling a static method is its class name followed by a dot separator (.) and the method name; for example:
ClassName.methodName( arguments );
The syntax for the static main method is a special case. The Java Virtual Machine (JVM) begins execution of an application with the static main method.
C. CLASS MATH
All Math class methods are static and they are called using the class name followed by a dot separator (.) and the method name “static method syntax.” For example:
package MathPackage;
public class MathClass {
public static void main(String[] args) {
int x = 9;
int y = 3;
System.out.println( Math.sqrt( x ) );
System.out.println( Math.pow( x, y ) );
} // end main
} // end MathClass
As shown in the above program, no Math objects were created before calling either method Math.sqrt or Math.pow.
D. JOPTIONPANE EXAMPLE
Static methods were designed for frequently used simple tasks; for example, coding dialog boxes. The following program is an example of using the static method JOptionPane to create simple window dialog boxes.
package JOptionPanePackage;
import javax.swing.JOptionPane;
/*
* Use JOptionPane.showXxxDialog when you only need simple window dialog.
*/
public class JOptionPaneClass {
public static void main(String[] args){
/*
* JOptionPane.showInputDialog
* uses a text field
* to get a string from the user.
*/
double numerator =
Double.parseDouble(JOptionPane.showInputDialog
( "This program computes a QUOTIENT. \n\n QUOTIENT =" +
" (NUMERATOR / DENOMINATOR) \n\n 1. Type a NUMERATOR," +
" e.g., 999.999 \n 2. then Click OK to continue; \n 3. or, Click" +
" CANCEL to EXIT.\n\n" ));
double denominator =
Double.parseDouble(JOptionPane.showInputDialog
( "1. Type a DENOMINATOR, e.g., 3.3 \n 2. then" +
" Click OK to continue; \n 3. or, Click CANCEL to EXIT.\n " ));
double quotient =
(numerator / denominator);
/*
* JOptionPane.showMessageDialog
* displays a one-button dialog.
*/
JOptionPane.showMessageDialog
( null, "QUOTIENT equals: \n " +quotient,
"Click OK to Exit.", JOptionPane.PLAIN_MESSAGE );
} // end main
} // end class JOptionPaneClass
III. STATIC BINDING
Static binding refers to all static method calls being resolved by the JVM at compile time.
IV. STATIC IMPORT
Static import improves program readability and reduces coding time.
A. Import one static member:
Use the following syntax to import one static member of a class.
import static java.packageName.ClassName.staticMemberName;
For example:
package MathPackage;
import static java.lang.Math.sqrt;
public class MathClass {
public static void main(String[] args) {
int x = 9;
System.out.println( Math.sqrt( x ) );
System.out.println( sqrt( x ) );
} // end main
} // end MathClass
B. Import all static members:
Use the following syntax to import all static members of a class.
import static java.packageName.ClassName.*;
Now you can call each Math class method by name without writing Math in front of every Math method. For example:
package MathPackage;
import static java.lang.Math.*;
public class MathClass {
public static void main(String[] args) {
int x = 2;
int y = 3;
System.out.println(pow( x, y) );
} // end main
} // end MathClass
V. REFERENCES
CS106A Computer Science I: Programming Methodology Lectures by Mehran Sahami, Associate Professor, Computer Science Department (450 Serra Mall, Stanford, CA: Stanford University, 2007).
Java How To Program Eighth Edition by Paul Deitel and Harvey Deitel (Upper Saddle River, N.J.: Pearson Education, Inc., 2010).






MultiQuote





|