Suppose you wanted to create a new object. Instead of having to create a new Java file, we can just create a static class inside of an outer class, and treat the static class as a new class. For example, I have a Foo class, and I need a new Object. I can just go ahead and add a static class Bar inside Foo, like this:
[source lang="java"]//Outer, regular, independent classpublic class Foo { //Inner, temporary class used for debugging. static class Bar { //... }}[/source]
I could then treat the Bar class as a independent class for Foo. A basic rundown looks like this:
[source lang="java"]public class Foo { //We treat this as a class of its own. private Bar bar; //WRONG, we don't want to do this. We do not try to link it to Foo: //private Foo.Bar bar static class Bar { //Basic rundown on making Bar useful. private boolean code; //Setter public void setCode(boolean value){ code = value; } //Getter public boolean getCode(){ return code; } } //Initialization public Foo(){ bar = new Bar(); } //How we obtain the Bar object. Again, we don't link it to Foo. public Bar getBar(){ return bar; } //WRONG, we don't want to do this: //public Foo.Bar getBar() {...}}[/source]
And this how we execute Bar as a demonstration:
[source lang="java"]public static void main(String[] args){ Foo foo = new Foo(); foo.getBar().setCode(true); boolean test = foo.getBar().getCode(); //...}[/source]
Thus, I have shown you the technique. From here, we can continue to test and debug, and when the Bar object is useful enough, we can then bring it out as a independent class (and giving it its own Java source file). A few questions:
- Is this technique useful enough?
- Would you use it? Why?
- How would you use it?






