CONTENTS
• I. JAVA STATIC TUTORIAL INTRODUCTION
• II. JAVA STATIC METHOD
• III. JAVA STATIC BINDING
• IV. JAVA STATIC IMPORT
• V. JAVA STATIC TUTORIAL REFERENCES
YOU WILL LEARN:
1. Static methods are methods that perform tasks without creating objects.
2. Static methods are designed for frequently used simple tasks.
3. How to create window dialog boxes.
4. An easy to read way of calling Math class methods.
I. JAVA STATIC TUTORIAL INTRODUCTION
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. JAVA 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.
Class Math Example Output:
3.0
729.0
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
JOptionPaneClass Example Output:
The output assumes you type in 9 for the Numerator, and 3 for the Denominator, which gives you the 3.0 Quotient.
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( sqrt( x ) );
}
//end main
}
//end MathClass
Import One Static Member Example Output:
3.0
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 all Math class methods by name without writing Math in front of any 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
Import All Static Members Example Output:
729.0
V. JAVA STATIC TUTORIAL REFERENCES
Java How To Program Eighth Edition by Paul Deitel and Harvey Deitel (Upper Saddle River, N.J.: Pearson Education, Inc., 2010).
YOU HAVE LEARNED:
1. Static methods are methods that perform tasks without creating objects.
2. Static methods are designed for frequently used simple tasks.
3. How to create window dialog boxes.
4. An easy to read way of calling Math class methods.
Elcric Otto Circle
-->
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to ELCRIC OTTO CIRCLE's Home Page"