What is the difference between inner class, nested class and anonymous class?

Asked

Viewed 5,620 times

9

I often read about these three types of classes when I’m researching something about java, but the truth is that it often causes some confusion about what each one of them really is and whether there’s any difference between them.

What are internal, nested and anonymous classes? One can define that they are the same thing among themselves or are different things?

1 answer

12


According to J Steven Perry of Makoto Consulting Group Nested class are all classes that are within another class.

Example:

public class calculadora {


public class soma{/* código */ }

}

This type of class is used to manipulate internal processing data, but is limited by the class that needs it. This class type is usually used when both classes are Closely linked. The nested class will have access to the primitive data declared by the class that called it.
Note: is a kind of inner class.

As the nested class has a scope, it is limited by the rules scope. For example, a member variable can be accessed only by an instance of the class (an object). The same is true for a nested class. Suppose you have the following relationship between a Manager and a nested class called Directreports, which is a collection of Employees who report to this Manager:

public class Manager extends Employee {  

    private DirectReports directReports; 

    public Manager() {
        this.directReports = new DirectReports();  
    }

    ...  

    private class DirectReports {  . . .  } 
}

Just as each Manager object represents a single human being, the Directreports object represents a collection of real people (employees) reporting to a manager (Manager). Directreports will be different from one Manager to another. In this case, it makes sense that it refers to the nested class Directreports in the context of Manager instance that closes it, so it was made private.

Similarly I used in the first example... imagine the calculator class:

public class Calculadora extends Calculos{
   private  Soma soma;

   public Calculadora(){
      this.soma = New Soma();
   }

   public class Soma {}
}

The sum is a set of calculations that I called internally and showed, and as it is private will only be accessed by the context class, in this case, the Calculator class.

And if I want to use public?

Example answered by @Maniero in another website post.

Can’t receive directly the same as private:

A reference to the mother class is required for this purpose create an instance for it and in this instance access the class internal. See this example I took from a tutorial on classes internal:

public class InnerClassTest {
    public void foo() {
        System.out.println("Outer class");
    }

    public class ReallyInner {
        public void foo() {
            System.out.println("Inner class");
        }

        public void test() {
            this.foo();
            InnerClassTest.this.foo();
        }
    }

    public static void main(String[] args) {
        InnerClassTest o = new InnerClassTest();
        InnerClassTest.ReallyInner i = o.new ReallyInner();
        i.test();
    }
}

Internal Classes:

inserir a descrição da imagem aqui

Image available in references

Let’s go one by one, the first has already been set,nested classes, which is an inner class and the most common of all.

Internal class Static

A static class has no access to class instance members encapsulated, only static members, have the same visibility of an outside class.

public class Calculadora {

  public static class Soma{
    int a = 1;
    int b = 2;
  }
}

In this example the sum class has two integer attributes, a and b:

  • a will always be 1
  • b will always be 2

because they’re static... they won’t change.

Internal classes of methods

Methods also have classes? Yes!

protected  void Calcular(){

    class Calculo{
        private int soma;

        public void setSoma(int soma) {
            this.soma = soma;
        }

        public int getSoma() {
            return soma;
        }
    }
}

In this case the class will be defined by the scope of the method, cannot be declared static, and the class can only be accessed by other classes of this method.

Anonymous class

Every class that is not explicitly declared but being called in code, represents the behavior of a class or an interface.

 protected  void Calcular(){
          int a,b;
          Calculo calculo = new Calculo;
          calculo.somar({
public void somei(){

}
});// parece o click listener

}

Starting from the example that there is not necessarily a class for the method to add, but , without statement it is there , ie anonymous classes are internal classes that function as a subclass of the object type, not where it is referenced (in the case of object of type sum).

One can define that they are the same thing among themselves?

Bottom line, Nested Class is a kind of inner class, which we commonly use, while the anonymous ones don’t, since it functions as a sub-call in the already instantiated method, is not common, and sometimes will make it difficult to read the code, since the class could have been declared before... So in general nested and anonymous classes are inner classes... and inner class is all those being called within some other class, method, or anonymously (without declaration) in a "parent" class call with a method in question.

References:

Nested class

Internal classes

Internal classes

Internal classes

Anonymous class

Anonymous inner class

  • Great answer! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.