Java is (null)

I want to know what that means?

public Settings() { this(null); } 

The above code is the constructor for the Settings class. What does (null) mean here?

+6
source share
8 answers
 public Settings() { this(null); //this is calling the next constructor } public Settings(Object o) { // this one } 

This is often used to pass default values, so you can use one constructor.

 public Person() { this("Name"); } public Person(String name) { this(name,20) } public Person(String name, int age) { //... } 
+12
source

This means that you are invoking an overloaded constructor that takes an Object sorts, but you are not passing an object, but just null .

+10
source

This is a constructor that calls another constructor in the same class.

You probably have something like this:

 public class Settings { public Settings() { this(null); // <-- This is calling the constructor below } public Settings(object someValue) { } } 

Often this template is used so that you can offer a constructor with fewer parameters (for the convenience of callers), but keep the logic contained in one place (calling constructor).

+4
source

It calls another constructor inside the Settings class. Find another constructor that takes a single parameter.

+4
source

It calls the default constructor, passing null as an argument ...

+1
source

Try to read the Overloaded constructor in java and you call the constructor that has only one Parameter ..

.

  public Settings() { this(null); } public Settings(Object obj){ } 
+1
source

This is called Constructor Chaining in Java . On this call, you actually invoke the overloaded constructor of the class object. For example

 class Employee extends Person { public Employee() { this("2") //Invoke Employee overloaded constructor"; } public Employee(String s) { System.out.println(s); } } 
+1
source

This basically calls another parameterized constructor in the same class as each one mentioned.

One thing that needs to be noted here will lead to an error if there is no constructor with parameterization.

I do not use passing this () with a null value. But it can be a tough question to ask someone. :)

0
source

Source: https://habr.com/ru/post/916116/


All Articles