Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction and usage of static keyword in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the introduction and usage of the static keyword in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "the introduction and usage of the static keyword in Java".

Calling static with a class name has two meanings:

1. It can be understood as the content that is public to the whole class.

two。 It can be understood that you can use it directly without creating an object.

Class Student {

Private String name

Private String no

/ / omit getter and setter here

Public static String school

Public static void main (String [] args) {

Student stu1 = new Student ()

Stu1.setName ("xxx")

Stu1.setNo ("1001")

Student.school = "Qianfeng"

Student stu2 = new Student ()

Student.school = "Qianfeng Education"

System.out.println (Student.school)

}

}

two。 When it modifies the method, the method does not need to be called by an object, but can be called directly using the class name.

/ / display only code snippets

Public static String getSchool () {

Return school

}

/ / call from other location

System.out.println (Student.getSchool ())

Note: normal properties cannot be called in the static method. You cannot use the this keyword either. Because the static method is called using the class name, and you can't tell whether to create an object or not, you can't call the corresponding method or property of the object at all, you can only call the property or method of static.

A code block refers to writing a piece of code directly in the middle of {} in a class, which does not need to be called manually, automatically every time an object is created, or even before the construction method.

Public class Student {

Private String name

Private String no

Public static String school

Public Student () {

System.out.println ("no parameter constructor")

}

{

System.out.println ("here is the code block")

}

}

3. When static decorates a code block, it is a static code block that is called when the class is loaded, and only once the first time it is loaded. There is no need to create an object. If the object is created, the call order is: first call the static code block, then the code block, and finally the constructor.

Public class Student {

Private String name

Private String no

Public static String school

Public Student () {

System.out.println ("no parameter constructor")

}

{

System.out.println ("here is the code block")

}

Static {

System.out.println ("here is a static block of code")

}

}

When there is a parent class Person and a subclass Student, each with constructors, code blocks, and static code blocks, create a subclass object in the following order:

Here is the Person static code block

Here is the Student static code block

Here is the Person code block

Person no-parameter constructor

Here is the Student code block

Student no-parameter constructor

The code is as follows:

Public class Person {

Public Person () {

System.out.println ("Person no-parameter constructor")

}

{

System.out.println ("this is the Person code block")

}

Static {

System.out.println ("here is the Person static code block")

}

}

Public class Student extends Person {

Public Student () {

System.out.println ("Student no-parameter constructor")

}

{

System.out.println ("this is the Student code block")

}

Static {

System.out.println ("here is the Student static code block")

}

}

To sum up: static actually translates classes, it is easier to understand, such as static modified attributes, called class attributes, static modified methods, called class methods.

At this point, I believe you have a deeper understanding of "the introduction and usage of the static keyword in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report