当前位置:网站首页>Difference between static method and non static method (advantages / disadvantages)

Difference between static method and non static method (advantages / disadvantages)

2022-07-04 07:48:00 Lucky_ mzc

Static methods static( Class method ) and Non static methods ( Example method ) The difference between :

There are three main methods :
1. Construction method
2. Non static methods ( Common method / Example method )
3. Static methods ( Class method )

One 、 The difference between static and non static methods ( Call object 、 Reference variables are different )
Static methods : It's using static Methods of keyword modification , Also called class method . Belonging to the class , Does not belong to the object , Before instantiating an object, you can use Class name . Method name Call static methods . ( Static attribute , Static methods belong to classes , Can be called directly through the class name ).
1. In a static method , You can call static methods .
2. In a static method , Non static methods cannot be called .
3. In a static method , You can reference class variables ( namely ,static Decorated variable ).
4. In a static method , Member variables cannot be referenced ( namely , No, static Decorated variable ).
5. In a static method , Out of commission super and this keyword .

Non static methods : It does not contain static Common methods of keyword modification , Also known as instance method , Member method . Belonging to an object , Not belonging to class .( Member attribute , Member methods belong to objects , Must pass new Keyword after creating an object , And then through the object call ).
1. In the ordinary way , You can call ordinary methods .
2. In the ordinary way , You can call static methods .
3. In the ordinary way , You can reference class variables and member variables .
4. In the ordinary way , have access to super and this keyword .

Two 、 The difference between static and non static methods ( The call method is different )

Static methods can call directly , Class name call and object call .( Class name . Method name / Object name . Method name )
But non static methods can only be called through objects .( Object name . Method name )

3、 ... and 、 The difference between static and non static methods ( Life cycle is different )

The life cycle of a static method is as long as the corresponding class , Static methods and static variables are allocated and loaded into memory as classes are defined . Until the end of the thread , Static properties and methods will be destroyed .( That is, static methods belong to classes )
The life cycle of a non static method is as long as the instantiated object of a class , Only when a class instantiates an object , Non static methods will be created , And when the object is destroyed , Non static methods are also destroyed immediately .( That is, non static methods belong to objects )

summary : Class methods can be called directly by the class name , Instance method must instantiate class first , Reinitialize the object , Then you can call... Through the instance object of the class .

give an example

class ABC{
  public static void main(String[] str){
      ABC.testStatic();         // Call... Directly through class 

      ABC a = new ABC();        // Instantiation , Then the constructor initializes 
      a.testMethod();           // Object call method 
  }

  public static void testStatic(){
	 System.out.println("This is static method");
  }

  public void testMethod(){
 	 System.out.println("This is instance method");
  }
}

static state static Variable / Method Initialized during class loading , There is only one copy in memory , So you can think of it as a global variable / Method .

advantage

  1. Class level , You don't need to create objects to use .
  2. Globally unique , Unique in memory , Static variables can uniquely identify certain states .
  3. Initialize when the class is loaded , Resident in memory , Call fast and convenient .

Application scenarios :
1. Static methods are best suited to the definition of methods in tool classes ; Like file manipulation , Date processing method, etc .
2. Static methods are suitable for the definition of entry methods ; As in the singleton mode , Because you can't get the constructor from the outside , It is very necessary to define a static method to obtain objects .
3. Static variables are suitable for the definition of global variables .( For example, Boolean static member variables are used as control characters )

shortcoming

  1. Static methods cannot call non static methods and variables .( Non static methods can call static methods arbitrarily / Variable
  2. Out of commission this and super keyword .( Belong to class level , No object created, check-in is not available this/super)
原网站

版权声明
本文为[Lucky_ mzc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141521017601.html