Wednesday, August 3, 2016

How to instantiate and use a static nested class in Java

In order to instantiate a static inner class in Java in another separate class you may need to use the outer class's name as shown in the example below :

abstract class Shape {
public static class Color {
int m_red, m_green, m_blue;

public Color() {
// call the other overloaded Color constructor by passing default
// values
this(0, 0, 0);
}

public Color(int red, int green, int blue) {
m_red = red;
m_green = green;
m_blue = blue;
}

public String toString() {
return " red = " + m_red + " green = " + m_green + " blue = " + m_blue;
}
// other color members elided
}
// other Shape members elided
}

public class TestColor {
public static void main(String[] args) {
// since Color is a static nested class,
// we access it using the name of the outer class, as in Shape.Color
// note that we do not (and cannot) instantiate Shape class for using
// Color class
Shape.Color white = new Shape.Color(255, 255, 255);
System.out.println("White color has values:" + white);
}
}

No comments:

Post a Comment