Monday, June 27, 2011

Function calls in Julius ASR

That's what I was feeling when I began to develop and modify in Julius code (an open source ASR system implemented by C language by a brilliant Japanese person).


When to use static nested classes?

It may be confusing at first that, which one to use when using nested classes? static nested classes or inner classes? and in this article I will answer this question for you.

first:
the use of terms:
A nested class is a class which is contained in another class at the source code level.
If you declare it with the static modifier, then its name is static nested class.
A non-static nested class is called inner class.

second:
the key differences between static nested classes and inner classes
static nested classes:
1. They do not have access to the fields and methods of the enclosing class.
2. they can be instantiated without a corresponding instance of the outer class.
inner classes:
1. inner class has an implicit reference to the enclosing class, so they have access to the fields and methods of the enclosing class even if they are declared private.
2. they cannot be instantiated without a corresponding instance of the outer class.

third:
when to use nested classes in general?
if you just want to keep your classes together if they belong typically together or if you need a class that do some functionality that is exclusively related to the outer class, The nesting creates a type of namespace. To denote a nested class from outside its enclosing class, the nested class is prefixed with the name of the enclosing class.

when to use static vs non static?
from an OO design point of view you decide which one to use depending on whether you want your nested class to have access to enclosing class members or no, so ask yourself, does your nested class need that access?
it is a better idea if you are using a nested class is to start off with it being static, and then decide if it really needs to be non-static based on your usage.

to fully understand the decision rule we can see a practical and real example that is in LinkedList class
note my comments beside the code.


public class LinkedList
{
transient int size = 0;

transient Node first; /*LinkedList consists of Nodes so we made it as nested class*/
transient Node last;
...

private class ListItr implements ListIterator { /* ListItr needs access to outer class members to iterate through them, it needs access to size, first and last members, so we made it inner class*/
private Node lastReturned = null;
private Node next;
private int nextIndex;
private int expectedModCount = modCount;
...
}

private static class Node { /*Node do not need access to the outer class members so it is declared as static, Node is the basic unit that consist the LinkedList*/
E item;
Node next;
Node prev;
...
}

} //outer class end

references:
Sybex SCJP for Java platform SE6, Richard F. Raposa
http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class
http://stackoverflow.com/questions/253492/static-nested-class-in-java-why
http://www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.html
http://www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.html