


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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
