In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use this and static keywords in Java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
1. This1.1 object creation process
The constructor method is an important way to create a Java object, and when the constructor is called through the new keyword, the constructor does return the object of this class, but this object is not entirely the responsibility of the constructor. Creating an object is divided into four steps:
1. Allocate object space and initialize object member variables to 0 or empty
two。 Perform explicit initialization of property values
3. Execute the construction method
4. Returns the address of the object to the relevant variable.
1.2 the nature of this
The essence of this is "the address of the created object"!
Because the object was created before the constructor was called.
Therefore, you can also use this to represent the "current object" in the constructor.
1.3Common usage of this
Where ambiguity occurs in the program, this should be used to indicate the current object
In a normal method, this always points to the object that calls the method
In the constructor, the this always points to the object being initialized.
Be careful
Use the this keyword to call the overloaded constructor to avoid the same initialization code. However, it can only be used in the constructor and must be in the first sentence of the constructor.
This cannot be used in static methods.
1.3.1 simple use of this package com.bigdataBC.demo;public class Student {int id; String name; int score; public Student () {} public Student (int id, String name) {System.out.println ("initializing already created objects:" + this); this.id = id; this.name = name } public void stu_info () {/ / whether or not adding this is the same. In this case, you can omit System.out.println ("student name:" + this.name+ "\ n" + "grade is" + score);} public static void main (String [] args) {Student student = new Student (001, "Zhang San"); System.out.println ("print" Zhang San "student object:" + student) Student.stu_info ();}} > > initializing the created object: com.bigdataBC.demo.Student@1b6d3586 prints "Zhang San" this student object: com.bigdataBC.demo.Student@1b6d3586 student name: Zhang San grade is 0Process finished with exit code 01.3.2 this () calls the overloaded constructor package com.bigdataBC.demo;public class Student {int id; String name; int score Student () {System.out.println ("initializing a student object");} Student (int id, String name) {this (); / / is used to call the no-parameter constructor. Note that it must be on the first line / * use Student (); it cannot be called and must be this () The following two points to member variables * / / are used to distinguish between member variables and local variables. This situation accounts for the majority of this usage! This.id = id; this.name = name; / * if this.var is not used here, it only points to the local variable rather than the member variable * / id=888; name= "Li Si";} Student (int id,String name,int score) {this (id,name); / / call the constructor with parameters, which must also be on the first line this.score=score } void stu_info () {/ / whether or not adding this is the same. In this case, you can omit System.out.println ("student name:" + this.name+ "\ t" + "grade:" + score);} void print_stu () {System.out.println ("print basic student information:"); this.stu_info () / / call the method in this class} public static void main (String [] args) {Student student = new Student; student.print_stu ();}}
The result is clear at a glance, hey!
Is about to initialize a student object to print basic student information: student name: Zhang San score: 7002. Overview of static2.1
In a class, member variables declared with static are static member variables, also known as class variables. The life cycle of a class variable is the same as that of a class and is valid throughout the execution of the application.
Characteristics
Is a common variable of the class, belongs to the class, is shared by all instances of the class, and is explicitly initialized when the class is loaded.
For all objects of this class, there is only one static member variable. Shared by all objects of this class!
Generally use "class name. class property / method" call. (static members can also be accessed through object references or class names (no instantiation is required). )
Non-static members cannot be accessed directly in the static method.
2.2 simple use of static package com.bigdataBC.demo;public class Student {int id; String name; / / static variable static String address= "Beijing"; public Student (int id, String name) {this.id = id; this.name = name;} public void test () {System.out.println ("Student name:" + name) } / / static method public static void static_test () {/ * if a non-static member is called in the static method, the compiler will report an error * * such as System.out.println ("student name:" + name) in the above test () method. * an error will be reported here because name is a non-static variable * * for example, call the test () method to test (); obviously, it will also report an error, because test is a non-static method * / System.out.println ("Student address:" + address) } public static void main (String [] args) {Student student = new Student (001, "Zhang San"); student.test (); / / static members can use the class name directly. Class attribute / method "call Student.static_test (); Student.address=" Shanghai "; Student.static_test ();}} > > Student name: Zhang San Student address: Beijing Student address: Shanghai Process finished with exit code 02.3 static initialization Block
Constructor is used for object initialization! Static initialization block, used for class initialization operation! Non-static members cannot be accessed directly in a static initialization block.
Static initialization block execution order
Back to the Object class, first execute the static initialization block of the Object, and then execute the static initialization block of the subclass down until the static initialization block of the class.
The execution order of the constructor is the same as this order
Package com.bigdataBC.demo;public class Student {int id; String name; / / static variable static String address; / / static block static {System.out.println ("perform class initialization"); address= "Beijing"; static_test ();} / / static method public static void static_test () {System.out.println ("Student address:" + address) } public static void main (String [] args) {Student student = new Student ();}} > > initialization of the execution class address: Beijing Process finished with exit code 0 after reading the above, do you have any further understanding of how to use the this and static keywords in Java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.