Home > Java > Javagetting Started > body text

The difference between static properties and dynamic properties in java

王林
Release: 2019-11-29 14:32:20
forward
4887 people have browsed it

The difference between static properties and dynamic properties in java

#Why can static be called without creating an object?

Before the object is created, all static properties or methods are placed in the metadata area, and the static ones can be called through the class name.

For example:

public class Cat{
    public static String name;//静态的,不创建对象都存在
    public String color; //动态的,不创建猫的对象不存在这个属性
}
Copy after login
public class Main{
    public static void main(String[] args){
        Cat c =new Cat();
        c.color = "黑色";
        c.name = "喵喵";
        Cat c1 = new Cat();
        c1.color = "蓝";
        System.out.println(c1.name); //正确 这是因为name是静态属性
    }
}
Copy after login

Online teaching video sharing: java teaching video

Characteristics of static attributes: All objects under this class share this one Attribute, this attribute still exists if the object is not created, so when modifying this attribute, just modify it through the class (Cat.name = "Xiao Meow")

Dynamic attributes only exist in the created object, and only create This property of the object exists.

The difference between static properties and dynamic properties:

The object can be found through the stack, so can it call the content in the metadata area? ——Yes

So can the content in the heap be called through a class? ——No, because there is a problem of certainty and uncertainty in this process.

Call the content of the metadata area through the class, because the object is dynamic, calling static things can ensure that the static things must exist when calling, and the static things exist before the existence of the object; from the static When calling something dynamic in the heap, it is not certain whether the dynamic thing must exist.

So the dynamic one can call the static one, but the static one cannot call the dynamic one.

public class Cat{
    public static String name;//静态的,不创建对象都存在
    public String color; //动态的,不创建猫的对象不存在这个属性
    public Cat(){
        
    }
    public void foo(){
        color = "黑";//正确
        name = "喵喵";//正确,一定可以调用静态的
        
    }
    public static void test(){
        color = "黑"; //错误,静态方法无法调用动态的属性
        name = "小喵";//正确
        foo();//动态的不一定存在
        Cat c = new Cat();
        c.foo(); //这时再能调用
    }
}
Copy after login

For more related articles and tutorials, please visit: java introductory learning

The above is the detailed content of The difference between static properties and dynamic properties in java. For more information, please follow other related articles on the PHP Chinese website!

source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template