In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to define the constructor of java". In daily operation, I believe many people have doubts about how to define the constructor of java. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to define the constructor of java". Next, please follow the editor to study!
Catalogue
1. Constructor
1.1 the permission modifier is public
1.2 display definition constructor
1. Constructor
Constructor, also known as constructor, constructor, is a special type of method that is responsible for initializing member variables (fields) in a class. The purpose of the constructor is to perform initialization when an object is created, and when an object is created, the system initializes an instance of that object by default.
There are two types of construction methods:
1. Default construction method (no parameter construction method)
2. Parameterized construction method
1.1 the permission modifier is public
The permission modifier is public, which means that the internal property performance is accessed by other classes.
The default constructor for java is unparameterized.
The Java compiler automatically creates a no-parameter constructor, so we can omit it in the class even if we don't have it. There is no need to assign values when instantiating objects
Create a new Person class and instantiate the person object with new. A Person.class object is generated after running.
Public class Demo02 {public static void main (String [] args) {/ / new instantiates an object / / A class has a method with the same method name as the class name, even if nothing is written. Person p=new Person (); create an object without assignment in parentheses. If there is no assignment, the default value of character type is null, and the default value of numeric type is 0}} public class Person {}
If nothing is written in the class, after running, there is one more Person () method by default. Here is the code for Person.class:
/ Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler) / / package Demo01;public class Person {public Person () {}}
So: even if a class does not write anything, it will still have a default method
Parametric construction method:
Parametric construction methods are used to provide different initialized values for different objects.
If a parameterized constructor already exists in the class, the compiler no longer provides the default parameterless constructor. You need to assign a value when instantiating an object, otherwise an error will be reported
1.2 display definition constructor package Demo01;public class Demo02 {public static void main (String [] args) {/ / new instantiates an object / / a class has a method with the same method name as the class name, even if nothing is written. Person p=new Person (); System.out.println (p.name); / / null}} package Demo01;public class Person {String name; / / instantiate the initial value / / 1, using the new keyword, essentially the constructor / / 2, used to initialize the value public Person () {/ / default constructor this.name= "xiaoming" } / / Parametric construction: once a parametric construction is defined, the definition must be displayed without parameter construction, otherwise it is invalid. It is also an overloaded public Person (String name) {this.name=name;} / / shortcut key: alt + insert to generate the constructor automatically. You can choose the constructor without parameters. }
Summary:
Constructor:
1. Same as the class name
2. No return value
Function:
1. New is essentially calling the constructor.
2. Initialize the value of the object
Note: after defining a construction, if you want to use a no-parameter construction, define a no-parameter construction as shown, otherwise you cannot use the no-parameter construction method ALT + INSERT to generate the constructor
At this point, the study on "how to define the constructor of java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.