Java in-depth learning of static static code blocks
Static code block
static
Can modify properties, methods, inner classes, and code blocks.
Static code blocks are called when the class is loaded, and are only executed once, and take precedence over various code blocks and constructors.
If static code is used in one place, then all static code blocks will be called.
Recommended related video tutorials: java free video tutorial
Examples are as follows:
Parent class
public class Human { static { System.out.println("2.父类中的静态代码块"); } public String name; public Human() { System.out.println("1.父类中的构造方法"); } public static void test() { System.out.println("6.父类中的静态方法"); } }
Subclass
public class Man extends Human { static { System.out.println("4.子类中的静态代码块"); } public Man() { System.out.println("3.子类中的构造方法"); } public static void doo() { System.out.println("5.子类中的静态方法"); } }
Main class
public class Main { public static void main(String[] args) { // Human human = new Man(); 输出2 4 1 3 //父类的类名调用父类的方法 //Human.test(); //输出2 6 在调用静态代码时,怕需要上面的静态代码块的内容,所以将其链接到内存 与子类无关 //子类的类名调用子类的方法 //Man.doo(); // 输出2 4 5 在调用子类的静态方法时,父类的静态代码块执行,与父类的静态代码块有关 //子类的类名调用父类的方法 //Man.test(); // 输出 2 6 注意这个能调test不是因为继承,test声明在了human里,子类可以调用父类的方法, //但是父类不能调用子类的方法 所以在Main中不可直达Man中的的方法 //子类调用了父类的方法,在父类的方法里又引用了子类里的静态方法 //Man.test(); 输出 2 6 4 5 } }
The content of the static code block is executed first (but only once), then the parent class constructor is executed, and then the subclass constructor is executed.
This article comes from the java introductory program column. Welcome everyone to communicate and learn together!
The above is the detailed content of Java in-depth learning of static static code blocks. 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











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

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.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.
