Question

I have a utility class called Util. Within it I have a MatrixUtil static inner class (among others). Where are inner classes located by convention?

Should MatrixUtil go after Util fields but before Util methods? I was unable to find a java convention on this. Perhaps I missed it in the API...

Anyways, this is the structure I am talking about.

public class Util
{
    public static final Vector3f myField;

    public static class MatrixUtil
    {
        public static void myMatrixUtilMethod()
        {
            return;
        }
    }

    public static float myUtilMethod()
    {
        return;
    }
}

Code works either way but I'd like to know the proper convention for this. Thanks.

Was it helpful?

Solution

I always go for the principle of least surprise.

When a developer opens your source they will probably be placed at the top of the source. So first comes the important stuff.

As they scroll down your source they should next see the less important stuff.

Finally, at the end they should find the unimportant stuff.

I leave to you the decision as to whether the inner class is important or less important or unimportant.

I would certainly consider inner classes pretty close to unimportant especially if they are private - however, if is some kind of factory class that creates and manipulates objects of that type then they could be considered important.

In your case - you seem to be using an inner class to sub-categorize methods - I would not use an inner class to do that, I would use a nested package such as com.....util.matrixutil.

OTHER TIPS

I think, the majority of the cases are:

  • Anonymous inner classes extracted into inner classes for better readability (i.e. java.util.ArrayList.SubList, JCF and Guava are full of these).
  • Small classes or interfaces related mostly to the enclosing class (i.e. java.util.Map.Entry).

I think it's the first time I see an utility inner class, but it's all up to the author. :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top