search
HomeJavaJavagetting StartedThe difference between static properties and dynamic properties in java

The difference between static properties and dynamic properties in java

Nov 29, 2019 pm 02:32 PM
javaDynamic propertiesthe differencestatic properties

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; //动态的,不创建猫的对象不存在这个属性
}
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是静态属性
    }
}

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(); //这时再能调用
    }
}

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!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools