Do I need to create a new Static class?

Looking through the documentation, I read some where this ViewHolder is a static class, but does it need to be created for a static class? In this example, they made a new one on it?, But in accordance with the concept, the new should not be done in a static class?

+3
source share
3 answers

You can build classes in only four ways:

  • Using the "new"
  • Using the Class.newInstance Class
  • Using the / factory method, which internally uses the new one to create a new instance
  • Using object.clone, if supported

1 and 3 are the most used

+1
source

This explains well :

. , . , .

 // creating an instance of the enclosing class 
 NestedClassTip nt = new NestedClassTip();


 // creating an instance of the inner class requires 
 // a reference to an instance of the enclosing class 
 NestedClassTip.NestedOne nco = nt.new NestedOne();


 // creating an instance of the static nested class 
 // does not require an instance of the enclosing class 
 NestedClassTip.NestedTwo nct = new NestedClassTip.NestedTwo();



public class NestedClassTip {
private String name = "instance name";
private static String staticName = "static name";

public static void main(String args[]) {
    NestedClassTip nt = new NestedClassTip();

    NestedClassTip.NestedOne nco = nt.new NestedOne();

    NestedClassTip.NestedTwo nct = new NestedClassTip.NestedTwo();
}

class NestedOne {
    NestedOne() {
        System.out.println(name);
        System.out.println(staticName);
    }
}

static class NestedTwo {
    NestedTwo() {
        System.out.println(staticName);
    }
} }

, , . , Java.

+1

, , , . , , .

0

All Articles