Page 1 of 1

JAVA STATIC TUTORIAL Rate Topic: ***** 1 Votes

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 09 September 2011 - 02:29 AM

*
POPULAR

JAVA
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).

Is This A Good Question/Topic? 7
  • +

Replies To: JAVA STATIC TUTORIAL

#2 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1906
  • View blog
  • Posts: 3,951
  • Joined: 11-December 07

Posted 09 September 2011 - 05:27 PM

Nice wee tutorial there. Here is another use: the static block. It's rarely used (and for good reason!)


public class Demo {
	
	static {
		System.out.print("In before the ");
	}
	
	public static void main(String[] args) {
		System.out.println("Hello World!");
	}

}


Was This Post Helpful? 0
  • +
  • -

#3 FamiHug  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 34
  • Joined: 03-December 10

Posted 10 September 2011 - 03:25 AM

Nice tut!

I think you should write more about "III. Static binding"
A little about "Static binding & Dynamic binding"

And using static is not "good" because it isn't OOP
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Posted 10 September 2011 - 02:25 PM

Overuse of static isn't good. However, static does have its purposes. Look at the Singleton pattern or the Math class. Using static as a substitute for good, OO practices is a bad idea.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1