static

 

††††††††††† also:

††††††††††††††† non-static coding outside the main(...) method

 

Terminology: static variables are also called "class variables."Meaning these are variables not defined inside a method, which would be a "method variable."

Variables defined in the class, outside any methods, but not static, would be an "instance variable."

 

static can appear on a class, a method, or a variable.

 

static cannot appear on a constructor or an interface.

 

Variables can be static final.

 

You canít change a static method to non-static while overriding it.

 

You can't change a non-static method to static while overriding it either.Note you can overload OK, changing from one to the other.The restrictions are just on overriding.

 

You can't declare an static variable in an instance method.Method variables only have an instance method lifetime scope. Static lives longer.

 

Non-static methods can directly reference static class variables with an optional Class qualifier. ie. x = StaticClassName.y;

 

No matter where their statements fall in the class's code, any static variables will always be initialized before non-static variables.See initialization order.

 

 

non-static coding outside the main(...) method

 

The fact that the main(...) method is static has always caused confusion for beginner Java programmers.†† There are several ways to easily create a program with non-static methods in which to place your everyday Java code. The same demonstration program below is repeated four times using four different approaches. The program creates a small centered frame with one button on it, which, when it is clicked, simply causes a line to be written to the console saying so.

 

(Approach 1)You can create an object of yourself and deal with the non-static issues in your constructor:

 

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 

public class Demo1 extends JFrame implements ActionListener{

JButton b1 = new JButton("Button 1");

JPanel p1 = new JPanel( );

†††††††††††††† ††††††††††††††††††††††††††††††

public static void main(String[ ] args)†† {

Demo1 frame = new Demo1 ( );

frame.setSize(300, 75);

frame.setVisible(true);

}

 

public Demo1 ( )†† {

Container cp = getContentPane( );†††

p1.add(b1);††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††† . . . .

cp.add(p1);

b1.addActionListener(this);†††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††

}

public void actionPerformed( ActionEvent e )†† {

if ( e.getSource( ) == b1 ) System.out.println( "Button 1 was clicked " );

}}

 

(Approach 2) You can create an object of yourself and deal with everything in your constructor:

 

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Demo2 extends JFrame implements ActionListener{

JButton b1 = new JButton("Button 1");

JPanel p1 = new JPanel( );

†††††††††††††† ††††††††††††††††††††††††††††††

public static void main(String[ ] args)†† {

Demo2 D2 = new Demo2 ( );

}

public Demo2 ( )†† {

JFrame frame = new JFrame( );

Container cp = frame.getContentPane( );†††

p1.add(b1);†††††††††††††††††††††††††††

cp.add(p1);

b1.addActionListener(this);†††††††††††††††††††††††††††††††

frame.setSize( 300, 75 );

frame.setVisible( true );††††††††††††††††††††

}

public void actionPerformed(ActionEvent e)†† {

if ( e.getSource( ) == b1 ) System.out.println( "Button 1 was clicked " );

}}

 

(Approach 3) You can create an object of yourself and do everything in an instance method which you call:

 

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Demo3 extends JFrame implements ActionListener{

JButton b1 = new JButton("Button 1");

JPanel p1 = new JPanel( );

†††††††††††††† ††††††††††††††††††††††††††††††

public static void main(String[] args){

Demo3 obj = new Demo3 ( );

obj.method( );

}

void method( ){

JFrame frame = new JFrame( );

Container cp = frame.getContentPane( );†††

p1.add(b1);†††††††††††

cp.add(p1);

b1.addActionListener(this);†††††††††††††††††††††††††††††††

frame.setSize( 300, 75 );

frame.setVisible( true );

}

 

public void actionPerformed(ActionEvent e) ††{

if ( e.getSource( ) == b1 ) System.out.println( "Button 1 was clicked " );

}}

 

(Approach 4) You can instantiate a static inner class and do the work there:

 

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 

public class Demo4 {

public static void main(String[ ] args){

DoTheWork dtw = new DoTheWork( );

}

static class DoTheWorkimplements ActionListener {

JButton b1 = new JButton("Button 1");

JPanel p1 = new JPanel( );

††††††††

public DoTheWork( ){

Container cp = this.getContentPane( );†††

p1.add(b1);†††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††††

cp.add(p1);

b1.addActionListener(this);†††††††††††††††

this.setSize( 300, 75 );

this.setVisible( true );

}

public void actionPerformed( ActionEvent e ){

if ( e.getSource( ) == b1 ) System.out.println( "Button 1 was clicked " );

}}†† }