static class static and normal class

static and normal classes

@ When to define static classes and ordinary classes:

Generally, this class is often called, and it is called very frequently. If it is defined as static, you don’t need to instantiate it every time. You can call it directly with the class. Like tool classes, it is generally defined as static, so that you don’t need to instantiate it every time. .

@Static variables are shared, there is only one in the entire JVM, and member variables are unique to each instance. Member variables are used by the instance itself, for example, if the name of the company is static, then all employees of the company share the company name

@ Is there any benefit to using a static class over a commonly used class:

1. You don't need to instantiate it every time you call it. It's convenient. (Disadvantage: The static definition will always occupy the memory. When the relative is created, it will create a piece of memory, which will always occupy the memory)

2. Use the class name to call directly (when calling a common class, you must first give him new before calling)

3 If you call a static method, you can use the class name. The method name can also directly write a method name

   public class qin{

       private static int num=10;

       public static void main(String[] args) {

       show();//qin.show(); //All is possible, direct class name. Method name can also be direct method name

       System.out.println(num);

       static void show(){

       int age=20;

       print();

       add();

       System.out.println("I am a static method!");

 }

1 Static variables cannot be defined in static methods, they must be defined globally (otherwise the program will report an error) see <1>

   As long as it is a static member variable, no matter whether the method is static or not, it must be declared as global (otherwise an error will be reported)      

   Or since this method has been defined as static, the methods or properties in it are also static, so there is no need to add static to see <2> 

   <1> public  class Dd {                                   

          int a; or       

    <2>  int a;   

            static int b = 0;

            public static int func1(){

                public static int func1()   

                         {

                      {   

  int b =0; //Remove static, because his current functc1() method is already static, which means that the method or attribute in it is also static.

  b++;                                           b++; 

  System.out.println(b);                         system.out.println(b); 

   return b;                                      return b;

   }                                               } 

       public static void main(String[] args){

// Dd d = new Dd();

  Dd.func1();

   

   }                                                }

 

   2,private static final String url="jdbc:sqlserver://127......."

      The effect of adding static is: no new is needed when calling, the direct class name.url

   3. If there is static in the program, the program will execute static first, and then execute other things. . .

   4 private static int num=10; When adding static, the variable is instantiated only once. When the variable is called repeatedly, it saves memory and can be shared by others.

   5 public class qin{

private static int num=10;

        static{      

           System.out.println("I am a static code block!");

}

public static void main(String[] args) {

   show();//qin.show(); //All is possible, direct class name. Method name can also be direct method name

   System.out.println(num);

static void show(){

   int age=20;

   print();

   add();

   System.out.println("I am a static method!");

}

 Note the following 6 points:

   //1. Instance methods cannot be called in static methods

   //2. Static methods can be called in instance methods

   //3. Static method call method: 1. Class name 2. Object

   //4. Instance method call method: 1. Object

   //5. Static methods can only contain static methods and non-static variables 

   //6. Instance methods can have static methods 

 

@ If it is a static class, it can be called directly without the need for a new object. For ordinary methods, a new object is required first.



 

 

 

 

     

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326176921&siteId=291194637