Java Static Member Classes
In this chapter you will learn:
- How to declare and use Static Member Classes
- Example - Java Static Member Classes
- Example - implementation for different data types
Description
A static member class is a static member of an enclosing class. A static member class cannot access the enclosing class's instance fields and invoke its instance methods.
A static member can access the enclosing class's static fields and invoke its static methods including private fields and methods.
Example
The following code has a static member class declaration.
class Demo {/*ww w . ja v a 2 s. co m*/
public static void main(String[] args) {
Main.EnclosedClass.accessEnclosingClass();
Main.EnclosedClass ec = new Main.EnclosedClass();
ec.accessEnclosingClass2();
}
}
class Main {
private static int outerVariable;
private static void privateStaticOuterMethod() {
System.out.println(outerVariable);
}
static void staticOuterMethod() {
EnclosedClass.accessEnclosingClass();
}
static class EnclosedClass {
static void accessEnclosingClass() {
outerVariable = 1;
privateStaticOuterMethod();
}
void accessEnclosingClass2() {
staticOuterMethod();
}
}
}
The static member classes can declare multiple implementations of their enclosing class.
Example 2
The following code declares a Rectangle
class and it uses static member class to
provide Rectangle
implementation for different data types, one is for double
type
and another is for float
type.
abstract class Rectangle {
abstract double getX();
/* w w w . j a v a 2 s. c o m*/
abstract double getY();
abstract double getWidth();
abstract double getHeight();
static class Double extends Rectangle {
private double x, y, width, height;
Double(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
double getX() {
return x;
}
double getY() {
return y;
}
double getWidth() {
return width;
}
double getHeight() {
return height;
}
}
static class Float extends Rectangle {
private float x, y, width, height;
Float(float x, float y, float width, float height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
double getX() {
return x;
}
double getY() {
return y;
}
double getWidth() {
return width;
}
double getHeight() {
return height;
}
}
private Rectangle() {
}
boolean contains(double x, double y) {
return (x >= getX() && x < getX() + getWidth()) && (y >= getY() && y < getY() + getHeight());
}
}
public class Main {
public static void main(String[] args) {
Rectangle r = new Rectangle.Double(10.0, 10.0, 20.0, 30.0);
r = new Rectangle.Float(10.0f, 10.0f, 20.0f, 30.0f);
}
}
Next chapter...
What you will learn in the next chapter:
- What are the three types of class variables
- How Java class member variables got initialized
- Example - Member variable
- How does automatic variable(method local variables) got initialized
- How does Class variable (static variable) got initialized
Java Class
Java Object
Java Object Reference Variable
Java Methods
Java Method Return
Java Method Parameters
Java Class Constructors
Java Default Constructor
Java Constructor Parameters
Java this Keyword
Java static keyword
Java Method Overload
Java Constructors Overload
Java Method Argument Passing
Java Method Recursion
Java Nested Class
Java Anonymous Classes
Java Local Classes
Java Member Classes
Java main() Method
Java Class Inheritance
Java super keyword
Java Method Overriding
Java Constructor in hierarchy
Polymorphism
Java final keyword
Java Abstract class
Java Class Access Control
Java Package
Java Packages Import
Java Interface
Java Interface as data type
Java interface as build block
Java instanceof operator
Java Source Files
Java Object
Java Object Reference Variable
Java Methods
Java Method Return
Java Method Parameters
Java Class Constructors
Java Default Constructor
Java Constructor Parameters
Java this Keyword
Java static keyword
Java Method Overload
Java Constructors Overload
Java Method Argument Passing
Java Method Recursion
Java Nested Class
Java Anonymous Classes
Java Local Classes
Java Member Classes
Java Static Member Classes
Java Class VariablesJava main() Method
Java Class Inheritance
Java super keyword
Java Method Overriding
Java Constructor in hierarchy
Polymorphism
Java final keyword
Java Abstract class
Java Class Access Control
Java Package
Java Packages Import
Java Interface
Java Interface as data type
Java interface as build block
Java instanceof operator
Java Source Files