Home  >  Article  >  Java  >  Summary on the use of static keyword and member methods and member attributes

Summary on the use of static keyword and member methods and member attributes

巴扎黑
巴扎黑Original
2016-12-05 10:38:301811browse

static means "static" or "global" and is used to modify variables, properties, and methods.

1. Static attribute [static domain]
When static modifies an attribute, it is an attribute shared by all objects of the class. It will only occupy a piece of memory space and can be called directly through the object or class name.


public static B b= new B();



2. Static method
When static modifies a method, since it is called directly through the class name, you can no longer use the this and super keywords, and you cannot directly access the instance variables and instance methods of the class to which it belongs. (Member variables and member methods without static). Whether the static call is to call the parent class or the subclass is only related to the class name.

public static void study() {  
        System.out.println(Student.name + "在学习");  
    }


Static method is a special type of method inside a class. The corresponding method is declared static only when needed. Methods inside a class are generally non-static

static attributes and methods are not You need to create an object to call.

a. A method does not need to access the object state, and its required parameters are provided through display parameters. [No need to create objects]
b. A method only needs to access the static domain of the class.


Through the understanding of static attributes and static methods, we summarized the usage of some member attributes and member methods:
Member attributes: Each object occupies a shared memory space separately and must be called through the object.
(Every time an object is new, member attributes are executed once)


public A a = new A();




Member method: Whether to call the parent class or override by the subclass is only related to the object itself


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:java regular expressionNext article:java regular expression