What is a static java class?

what is a static java class?

Search for: what is a static java class?

Here is an article from java world

A top-level class is by definition already top-level, so there is no point in declaring it static; it is an error to do so. The compiler will detect and report this error.

To use the nested top-level class, write: TopLevelClass.NestedClass.

a static inner class doesn't have access to local members of its outer class



//A static nested class
OuterClass.NestedStaticClass printer = new OuterClass.NestedStaticClass();

//Outer class
OuterClass outer = new OuterClass();        

//A nostatic inner class
OuterClass.InnerClass inner  = outer.new InnerClass();

See that, being a static class, (much like a static variable), it doesn't need a this pointer of the outer class.

Perhaps to give them visibility outside and modularize the code ... (Not sure on my part what would you loose if it is outside?? Perhaps readability, visibility? May be!

Perhaps if we see the definition of a static function. A static function does not have the concept of a "this" instance pointer.

An inner class has a hidden this pointer.

An inner class that won't have a hidden this pointer, then, by extension be called static I guess!


public static interface
ValueAnimator.AnimatorUpdateListener
{
}

public class TestBasicValueEvaluator 
{
   private String tag = "TestBasicValueEvaluator";
   public  void test()
   {
      Log.d(tag, "Setting up the evaluator");
      ValueAnimator anim = ValueAnimator.ofInt(10, 200);
      anim.setDuration(200); //100 milliseconds
      ValueAnimator.setFrameDelay(5);
      anim.addUpdateListener(
             new ValueAnimator.AnimatorUpdateListener()  {        
                 public void onAnimationUpdate(ValueAnimator animation) { 
                    dosomethingWiththeValue(animation);
                 }    
             }
         );
      anim.start();
   }
   private void dosomethingWiththeValue(ValueAnimator aimator)
   {
        Integer value = (Integer) aimator.getAnimatedValue(); 
        Log.d(tag,"Value from the animator:" + value);
   }
}

//a.java: filename

//public class visible to all
public class a {}

//a private class visible to only 'a'
class b {}

//this is almost like 'b' being private static inner class

More on java inner classes

Search for: More on java inner classes

is that the private classes prevent another public class being named the same hence cluttering it.

Very many parent classes to hold on to many child classes with the same name but with different meaning and behaviors. Like iterators for example.

for example List.Iterator may be different from Map.Iterator. without the inner class naming you may have to create


List
ListIterator
Map
MapIterator

A solid underpinnings of the inner classes. A very good read


use a static final in scope variable like the passed in argument 
use the local variables of the enclosed class
Hold on to the enclosed class 
distinguish between its 'this' and the enclosed 'this'
the enclosed this is used as 'parentclass.this.variablex'

Private class
Inner class
Static inner class
Anonymous inner class
Lambda expressions