Java Technique: Using static inner class for debug purposes, then bring it out as a independent class for release

Started by
3 comments, last by Bruno Sofiato 11 years, 4 months ago
This is merely a discussion of a technique I came up with, and I wanted to talk about it before I really take it far.

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 class
public 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:

  1. Is this technique useful enough?
  2. Would you use it? Why?
  3. How would you use it?
Advertisement
Why is it difficult to create a completely new, independent class in Java? Personally, I think you'd be better off making it simpler to create new classes than developing techniques to work around how troublesome it is.
I don't really understand how this is related to debugging. It sounds like you use this technique to prototype designs quickly.

I don't really understand how this is related to debugging. It sounds like you use this technique to prototype designs quickly.


Is that how I should describe this technique? Yes, I believe, I just don't know how to describe it prior to your post.
I can understand not using, for debug purposes, methods (as the JVM debuggers code hotswap facilities is unable to change class structure). But I really don't see how this technique make it easier to debug.

Just create the new class on its own source code, if the class turn out to be worthless, delete it ;) my 2 cents

This topic is closed to new replies.

Advertisement