[Java] Nested class

Nested Class

static member class

public class MyClass{...}
class MyHelper{...}
//MyHelper definition under class block
public class MyClass {
  //Definition of static member class
  //Members cannot be seen from outside My Class
  private static class MyHelper {
    public void show() {
      System.out.println("Nested class is running!");
    }
  }
  public void run() {
    //MyHelper can be called inside the enclosing
    var helper = new MyHelper();
    helper.show();
  }
}
public class NestBasic {
  public static void main(String[] args) {
    var c = new MyClass();
    c.run();
    //MyHelpe is invisible from outside MyClass
    // var h = new MyClass.MyHelper(); //error!
  }
}

Non-static member class (= inner class)

class MyClass {
  private String str1 = "Enclosing instance";
  private static String str2 = "Enclosing class";

  private class MyHelper {
    private String str1 = "Nested instance";
    private static final String str2 = "Nested class";

    public void show() {
      //Inner class has a reference to the enclosing object
      //MyClass.Accessable with this
      System.out.println(MyClass.this.str1); //Enclosing instance
      System.out.println(MyClass.str2); //Enclosing class
    }
  }

  public void run() {
    //An instance of the inner class is created by the instance method of the enclosing class.
    var helper = new MyHelper();
    helper.show();
    //Access inner class from enclosing object
    System.out.println(helper.str1); //Nested instance
    System.out.println(MyHelper.str2); //Nested class
  }
}

public class NestedAccess {
  public static void main(String[] args) {
    var c = new MyClass();
    c.run();
  }
}

Example of non-static member class (inner class)

java:java.util.AbstractList.java


package java.util;
import java.util.function.Consumer;
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
//...Omission

    /*Iterators
No need to show the internal implementation of the Itr class
It would be nice if the Iterators type, which is the implementation source, could be seen from the outside.
The return value of the iterator method is an Iterator type
    */
    public Iterator<E> iterator() {
        return new Itr();
    }
//...Omission

    /*Inner class
Implement an iterator that accesses the private field in the list and accesses the subordinate elements in order
Iterator is defined separately as an Iterator implementation class
    */
    private class Itr implements Iterator<E> {
//...Omission
    }
//...Omission
}

Anonymous class (anonymous class)

//Event listener registration
btn.setOnClickListener(
  //Anonymous class(Event listener)Definition
  new View.OnClickListener(){
    //btn Click to display current time in txt
    @Override
    public void onClick(View view){
      TextView txt = findViewById(R.id.txtResult);
      txt.setText(LocalDateTime.now().toString());
    }
  }
);

Local class

Recommended Posts

[Java] Nested class
[Java] Class inheritance
java Scanner class
Java HashMap class
Java anonymous class
About Java class
[java] abstract class
[Java] Object class
Java local class
About class division (Java)
About Java StringBuilder class
[Java] About Singleton Class
Java inner class review
Java class type field
Java programming (class method)
About Java String class
Nested class (for myself)
Java programming (class structure)
About java abstract class
[Java] Integer wrapper class reference
Java memo (standard class) substring
Java tips --StaticUtility class modifier
Java inflexible String class substring
Java memo (standard class) length
[Implementation] Java Process class notes
About Java class loader types
How to use java class
[Java] Comparator of Collection class
Java class definition and instantiation
Java
Java learning memo (abstract class)
Summary of Java Math class
[Java] What is class inheritance?
About Java class variables class methods
Java
[Java basics] What is Class?
Create an immutable class with JAVA
Call Kotlin's sealed class from Java
[Java] Object-oriented syntax --class method / argument
Various methods of Java String class
[Java] Memo for naming class names
How to disassemble Java class files
Kotlin Class to send to Java developers
StringBuffer and StringBuilder Class in Java
How to decompile java class files
[Java] How to use LinkedHashMap class
java (use class type for field)
Java source code reading java.lang.Math class
[Java] Button template class by Graphics
How to use class methods [Java]
[Java] How to use Math class
Java learning (0)
Studying Java ―― 3
[Java] array
What is a class in Java language (3 /?)
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch