Home > Java > javaTutorial > body text

Java nested class Android development must know

怪我咯
Release: 2017-04-05 16:17:13
Original
1833 people have browsed it

A nested class refers to a class defined inside another class. The nested class exists only for its outer class. There are four types of nested classes: static member classes, non-static member classes, anonymous classes, and partial classes.

  • #Static member class is the simplest kind of nested class. It's better to think of it as a normal class that just happens to be defined inside another class. Static member classes can access all members of the enclosing class, including those private variables. Static member classes also comply with accessibility rules. If it is positioned private, it means that it can only be accessed inside the enclosing class.

    public class PhotoListAdapter extends RecyclerView.Adapter<PhotoListAdapter.ImageVH> {
    static class ImageVH extends RecyclerView.ViewHolder {
           //代码省略
        }
    }
    Copy after login
    1. In Android We are most familiar with listview's ViewHolder, which we usually use static member classes, usually as a public auxiliary class. Cache the layout in the view as a cache variable of the Listview.

    2. Private static member class, used to represent the components of the object represented by the peripheral class.

      The code is as follows

  • The syntax difference between non-static member classes and static member classes lies in whether there is the modifier static. Although their syntax is similar, the two are very different. Each instance of a non-static member class is implicitly associated with an enclosing instance of the enclosing class. Within the instance methods of a non-static member class, methods on the enclosing instance can be called. Only instances of static member classes can exist independently in the enclosing class. , an instance of a non-static member class cannot be created without an surrounding instance.

    When an instance of a non-static member class is created, the association between it and the surrounding instance is also established, and this association cannot be modified later. This association usually occurs when an instance method of the enclosing class calls the constructor of a non-static member class. This association consumes the space of non-static member class instances and increases the time overhead of construction. If the member class does not need to access the peripheral class instance, please add the static modifier.

    public class PhotoListAdapter extends RecyclerView.Adapter<PhotoListAdapter.ImageVH> {
     class ImageVH extends RecyclerView.ViewHolder {
               // 真是对外部类的实例持有,在Android开发中很大部分activity中的context的内存泄露因为这个原因。
        }
    }
    Copy after login
  • Anonymous class is different from other syntax units in JAVA. Anonymous class has no name. It is not a member of the peripheral class. It is not declared together with other members, but is used is declared and instantiated at the same time. Anonymous classes can appear anywhere in the code where an expression exists. Only if an anonymous class appears in a non-static environment, it will have a peripheral instance. Even if it is declared in a static environment, it cannot have any static members.

    imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       // 代码省略
                    }
                });
    Copy after login
  • Local classes are declared anywhere "local variables can be declared". Also comply with the scope rules, 1. Local classes have names that can be reused. 2. Unlike anonymous classes, only when the local class is defined in a non-static environment, it can have peripheral instances and cannot contain static members.

    public class mainActivity extends Activity{ 
    private Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
               // 此处同样可能引起context持有导致的内存泄露
            }
        };
      }
    Copy after login

The above is the detailed content of Java nested class Android development must know. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template