The difference between class methods and object methods is as follows:
1. Object methods must be called with objects, and class methods must be called with classes
2 , Properties (member variables) can be directly accessed in object methods, but properties (member variables) cannot be directly accessed in class methods
3. Class methods and object methods can call each other
3.1 Object Class methods can be called directly in methods
3.2 Object methods can be called indirectly in class methods (Note: This use is not recommended)
3.3 Other class methods can be directly called in class methods
3.4 Object methods can be called directly in object methods
4. Class methods belong to this class and will not change due to different objects being created. Class methods are loaded as the bytecode file of the class is loaded; The object method belongs to an object created by the current class and will change with the different objects created
Free learning video sharing:java learning video
Simple example sharing:
Application scenarios of class methods:
If attributes (member variables) are not used in the method, then use class methods if they can be used. The execution efficiency of methods is higher than that of object methods.
Class methods are generally used to define tool methods, such as:
String search, file operations, and database operations.
Related article tutorial sharing: Getting started with java
The above is the detailed content of The difference between class methods and object methods in java. For more information, please follow other related articles on the PHP Chinese website!