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

What are the differences between constructors and methods in Java

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what are the differences between constructors and methods in Java. I hope you will get something after reading this article. Let's discuss it together.

To learn Java, you must understand constructors. Because constructors can provide many special methods, this is often confused for beginners. However, there are many important differences between constructors and methods.

We say that the constructor is a method, just as the platypus in Australia is a kind of feeding animal. (press: foreigners like to use metaphors, so I will translate them.) To understand the platypus, you must first understand the difference between it and other breeders. Similarly, to understand constructors, you need to understand the difference between constructors and methods. For all java students, especially those who want to take a certification exam, it is important to understand the constructor. The following is a brief introduction, and finally a simple summary is made with a table.

Differences in function and function

The constructor is to create an instance of a class. This process can also be used when creating an object: Platypus p1 = new Platypus ()

Instead, the purpose of the method is to execute java code.

Modifier, the return value is different from the named

There are three convenient differences between constructors and methods: modifiers, return values, and naming. Like methods, the constructor can have any access decorations: public, protected, private or no decorations (usually called by package and friendly). Unlike the method, the constructor cannot have the following non-access modifications: abstract, final, native, static, or synchronized.

The return type is also very important. The method can return any type of value or no return value (void), and the constructor does not return a value and does not need a void.

Finally, let's talk about the naming of the two. The constructor uses the same name as the class, but the method is different. By convention, methods usually start with lowercase letters, while constructors usually start with uppercase letters. The constructor is usually a noun because it is the same as the class name, while the method is usually closer to the verb because it indicates an operation.

The usage of "this"

There is a big difference between constructors and methods that use the keyword this. The method reference this points to an instance of the class that is executing the method. Static methods cannot use the this keyword, because static methods are not instances of the class, so this has nothing to point to. The constructor's this points to another constructor with a different parameter list in the same class. Let's take a look at the following code:

Public class Platypus {String name; Platypus (String input) {name = input;} Platypus () {this ("John/Mary Doe");} public static void main (String args []) {Platypus p1 = new Platypus ("digger"); Platypus p2 = new Platypus ();}}

In the above code, there are two constructors with different parameter lists. The first constructor assigns a value to the member name of the class, and the second constructor calls the first constructor to give the member variable name an initial value "John/Mary Doe".

In the constructor, if you want to use the keyword this, you must put it on the first line, or failure to do so will result in a compilation error.

The usage of "super"

Constructors and methods both point to the superclass with the keyword super, but in a different way. Method uses this keyword to execute methods in the overloaded superclass. Look at the following example:

Class Mammal {void getBirthInfo () {System.out.println ("born alive.");} class Platypus extends Mammal {void getBirthInfo () {System.out.println ("hatch from eggs"); System.out.print ("a mammal normally is"); super.getBirthInfo ();}}

In the above example, super.getBirthInfo () is used to call the overloaded method in the superclass Mammal.

The constructor uses super to invoke the constructor in the superclass. And this line of code must be placed on the first line, otherwise the compilation will go wrong. Look at the following example:

Public class SuperClassDemo {SuperClassDemo () {}} class Child extends SuperClassDemo {Child () {super ();}}

In the above meaningless example, the constructor Child () contains super, and its purpose is to instantiate the constructor SuperClassDemo in the superclass and add it to the Child class.

The compiler automatically adds code

The compiler automatically adds code to the constructor, which may be confusing for novice java programmers. When we write a class without a constructor and compile it, the compiler automatically adds a constructor without parameters, such as: public class Example {}

After compilation, the code is as follows:

Public class Example {Example () {}}

If super is not used in the first line of the constructor, the compiler automatically adds, for example:

Public class TestConstructors {TestConstructors () {}}

The compiler adds code as follows:

Public class TestConstructors {TestConstructors () {super;}}

If you think about it, you will know the following code

Public class Example {}

The process will be added by the compiler, such as:

Public class Example {Example () {super;}}

Inherit

Constructors cannot be inherited. A subclass can inherit any method of a superclass. Look at the following code:

Public class Example {public void sayHi {system.out.println ("Hi");} Example () {}} public class SubClass extends Example {}

The class SubClass automatically inherits the sayHi method in the parent class, but the constructor Example () in the parent class cannot be inherited.

After reading this article, I believe you have a certain understanding of "what are the differences between constructors and methods in Java". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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