Home java Local and internal classes

Local and internal classes




The question arose – what is the difference between local and internal classes?

Answer 1, Authority 100%

Domestic classes are associated not with the outer class itself, but with its instance, i.e. Each internal class instance is associated with an instance of its surrounding class. You cannot create an internal class instance without binding to an external class instance. That is, the appearance instance must be created first, and only then you can create an internal class instance.

Public Class Outer {
  Private int Outint = 10;
  Class Innerclass {
    int getOutint () {
  Innerclass GetinnerClass () {


Public Static Void Main (String [] Args) Throws java.lang.Exception {
    Outer Outer = New Outer ();
    Outer.innerclass Innerclass = Outer.getinnerclass ();
    System.out.PrintLN ("GetOutint =" + innerclass.getOutint ());

Like other class fields, invested classes can be announced as Private , Public , Protected , or Package Private

At the same time, not static classes have access to the fields of the class, even if they are declared as Private . Static not have access to the members of the external class.

Public Class OuterClass {
  Public Void Method () {...}
  Public Class Innerclass {
    Public Void Method () {...}
    Public void Anothermethod () {
      Method ();

Call Method () From Anothermethod contact the Innerclass class method. To refer to the framing class method, you must use the design of the OuterClass.this.method ()

Local classes are defined in the Java code block. In practice, the announcement is most often occurs in a method of some other class. Although it is possible to declare a local class inside static and non-static initialization units.
Local class has access to class members in which it is announced

Public Class Outer {
  Void Printvars () {
    Final int i = 10;
    Class Local {
      String Strl = "Local";
      Void PrintLocal () {
        System.out.PrintLN ("Strl:" + Strl);
        System.out.printLN ("STRO:" + STRO);
        System.out.printLN ("Finalint:" + i);
    Local Local = New Local ();
    local.printLocal ();


public static void main (String [] args) throws java.lang.Exception {
    Outer o = new Outer ();
    o.printVars ();

Also, local class has access to the local variables. Local classes have access only to variables declared as a final .

However, since Java SE 8 , local classes have access to the final (final) local variables and parameters as well as immutable (effectively final) variable, ie, variables that have not changed since initialization.

i.e. in the previous example, you could not write final int i = 10; , but simply int i = 10; , assuming that it will not continue to happen to her nothing, not even the banal i = 11; . In this case System.out.println ( "finalInt:" + i); will generate an error


In fact, the limitations of local classes:

  • They are visible only within the block in which the declared;
  • They can not be declared as private , public , protected or static ;
  • they cannot have static ads (fields, methods, classes); The exceptions are constants (Static Final );

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions