static nested class java o que é code example

Example 1: Nested classes in java

// anonymous inner class as method argument.
interface HelloWorld
{
   String print();
}
public class AnonymousInnerDemo
{
   // method accepts object of interface HelloWorld
   public void display(HelloWorld m)
   {
      System.out.println(m.print() + "example of anonymous inner class as method argument.");
   }
   public static void main(String[] args)
   {
      AnonymousInnerDemo obj = new AnonymousInnerDemo();
      // pass an anonymous inner class as an argument
      obj.display(new HelloWorld() {
         public String print() {
            return "Hi! ";
         }
      });
   }
}

Example 2: use of nested class in java

In Java, it is possible to define a class within another class, such 
classes are known as nested classes. They enable you to logically group 
classes that are only used in one place, thus this increases the use of 
encapsulation, and creates more readable and maintainable code.

Tags:

Misc Example