Home>Article>Java> what is static method java

what is static method java

little bottle
little bottle Original
2019-05-25 16:46:09 14148browse

what is static method java

#Java is the same as C. If the static keyword is added before a class method, the method is called a static method, otherwise it is an instance method.

Static methods are owned by classes and can be used through objects or classes.

But it is generally recommended to use it through the class name, because static methods can be used as long as the class is defined and there is no need to create an instance of the class.

The difference between static methods and instance methods is mainly reflected in two aspects:

1. When calling static methods externally, you can use "The method of "class name.method name" can also be used. The method of "object name.method name" can also be used. Instance methods only have the latter method. In other words,calling static methods does not require creating an object.

2.When static methods access members of this class, they are only allowed to access static members (i.e. static member variables and static methods), but are not allowed to access instance member variables and instance methods; Instance methods do not have this restriction.

Static methods can only access static members, and instance methods can access static and instance members.

The reason why static methods are not allowed to access instance member variables is because instance member variables belong to a certain object, and the object does not necessarily exist when a static method is executed.

Similarly, because instance methods can access instance member variables, if a static method is allowed to call instance methods, it will indirectly be allowed to use instance member variables, so it cannot call instance methods.

Based on the same reason, the keyword this cannot be used in static methods.

The above is the detailed content of what is static method java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:What is a listener in java Next article:What is a listener in java