What is a static java class?

satya - 10/13/2013 10:22:39 AM

what is a static java class?

what is a static java class?

Search for: what is a static java class?

satya - 10/13/2013 10:23:44 AM

Here is an article from java world

Here is an article from java world

satya - 10/13/2013 10:25:01 AM

A top level static class is not allowed...

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.

satya - 10/13/2013 10:28:00 AM

Declare a nested class as static to expose it outside...

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

satya - 10/13/2013 10:31:58 AM

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

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

satya - 10/13/2013 10:35:03 AM

Apparently you can do this



//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.

satya - 10/13/2013 10:36:22 AM

So why do you create static inner classes?

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!

satya - 10/13/2013 10:38:58 AM

why are these classes called static?

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!

satya - 7/27/2014 5:56:49 PM

Typically in Android the listener classes are inner and static, Example:


public static interface
ValueAnimator.AnimatorUpdateListener
{
}

satya - 7/27/2014 5:57:39 PM

An example of a static inner class used as an anonymous inner class


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);
   }
}

satya - 7/27/2014 6:07:15 PM

You can have a non public class in a single java file along with a public class


//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

satya - 7/27/2014 6:08:50 PM

More on java inner classes

More on java inner classes

Search for: More on java inner classes

satya - 7/27/2014 6:14:47 PM

One apparent disadvantage of the classes in the same file

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

satya - 7/27/2014 6:18:53 PM

More subtly speaking the inner classes allow

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

satya - 7/27/2014 6:24:17 PM

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

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

satya - 7/27/2014 6:44:04 PM

An anonymous inner class can


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'

satya - 7/27/2014 6:46:54 PM

Variations


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