Java static inner classes java static inner classes

java static inner classes

Just a simple record. Because this thing has exclusive java. java with c ++ ratio is a very good language, at least the memory management so things do not bother to worry about. But the rise of scripting languages ​​and constantly compared, inefficient too. In any case do java android or need, so what you encounter any surprise.

Since the use of static inner classes, then why not take a look at non-static inner classes:

package com.company;

class Company {

    private String theCEO = "stupid";
    private static String companyName = "STUPID COM";

    class Employee {
        public Employee() {
            // 1
            System.out.println("company name - " + companyName);
            // 2
            System.out.println("CEO - " + theCEO);
        }
    }

    public Company(){
        System.out.println("Company object is created");
    }
}

public class Main {

    public static void main(String[] args) {
        // 3
        Company company = new Company();
        // 4
        Company.Employee employee = company.new Employee();
    }
}

First, create a named Companyclass, this class has a companyNamestatic member and a theCEOnon-static member. The company name is still two goods company, the company has two goods two goods of a non-static CEO. Then the two goods company to put a non-static staff.

  1. The employees can access static company name.
  2. The employees also have access to non-static CEO.
  3. To initialize the employees, it must first initialize the target company.
  4. The initial exchange with the staff is not new new , but company.new . It is a company subject .new .

Too much trouble, if not static inner classes. Use this inner class must be initialized parcel. Also, if it does not initialize the corresponding parcel, the internal class object how to access these parcels of non-static class object instance of the object it.

So it is still good with a static class:

package com.company;

class Company {

    private String theCEO = "stupid";
    private static String companyName = "STUPID COM";

    // 1
    static class Employee {
        public Employee() {
            System.out.println("company name - " + companyName);
            // 2
            //System.out.println("CEO - " + theCEO);
        }
    }

    public Company(){
        System.out.println("Company object is created");
    }
}

public class Main {

    public static void main(String[] args) {
//        Company company = new Company();
//        Company.Employee employee = company.new Employee();

        // 3
        Company.Employee employee = new Company.Employee();
    }
}

Or a two-goods company and the company's CEO and a staff of two goods.

  1. Plus static, the staff became like static inner classes.
  2. Static inner classes can not access non-static member wrapped class.
  3. It looks normal initialization method: new Company.Employee().

In general, inner classes do not need to access non-static member class inclusions of the time. Or just a tool to make external calls, and these tools need to be divided into zones according to function when using static inner classes.

Just a simple record. Because this thing has exclusive java. java with c ++ ratio is a very good language, at least the memory management so things do not bother to worry about. But the rise of scripting languages ​​and constantly compared, inefficient too. In any case do java android or need, so what you encounter any surprise.

Since the use of static inner classes, then why not take a look at non-static inner classes:

package com.company;

class Company {

    private String theCEO = "stupid";
    private static String companyName = "STUPID COM";

    class Employee {
        public Employee() {
            // 1
            System.out.println("company name - " + companyName);
            // 2
            System.out.println("CEO - " + theCEO);
        }
    }

    public Company(){
        System.out.println("Company object is created");
    }
}

public class Main {

    public static void main(String[] args) {
        // 3
        Company company = new Company();
        // 4
        Company.Employee employee = company.new Employee();
    }
}

First, create a named Companyclass, this class has a companyNamestatic member and a theCEOnon-static member. The company name is still two goods company, the company has two goods two goods of a non-static CEO. Then the two goods company to put a non-static staff.

  1. The employees can access static company name.
  2. The employees also have access to non-static CEO.
  3. To initialize the employees, it must first initialize the target company.
  4. The initial exchange with the staff is not new new , but company.new . It is a company subject .new .

Too much trouble, if not static inner classes. Use this inner class must be initialized parcel. Also, if it does not initialize the corresponding parcel, the internal class object how to access these parcels of non-static class object instance of the object it.

So it is still good with a static class:

package com.company;

class Company {

    private String theCEO = "stupid";
    private static String companyName = "STUPID COM";

    // 1
    static class Employee {
        public Employee() {
            System.out.println("company name - " + companyName);
            // 2
            //System.out.println("CEO - " + theCEO);
        }
    }

    public Company(){
        System.out.println("Company object is created");
    }
}

public class Main {

    public static void main(String[] args) {
//        Company company = new Company();
//        Company.Employee employee = company.new Employee();

        // 3
        Company.Employee employee = new Company.Employee();
    }
}

Or a two-goods company and the company's CEO and a staff of two goods.

  1. Plus static, the staff became like static inner classes.
  2. Static inner classes can not access non-static member wrapped class.
  3. It looks normal initialization method: new Company.Employee().

In general, inner classes do not need to access non-static member class inclusions of the time. Or just a tool to make external calls, and these tools need to be divided into zones according to function when using static inner classes.

Guess you like

Origin www.cnblogs.com/lev1/p/11198738.html