Question about Java class constructor

Can someone tell me what that means? I go through a Java book and I included this example:

public class Message { Message(){} public Message(String text){ this.text = text; } 

What does Message(){} Mean?

+6
java
source share
3 answers

This is a batch empty empty constructor with no arguments.

You can use it to create a new message instance from any code in the same package using new Message(); .

It is worth knowing that it does not initialize the text field, so it will hold the default value of null .

+10
source share

as

 Message() { } 

but using fewer lines.

the access level for it is the access level to the package (by default), which means that only classes within the same package can create this object using this constructor.

+1
source share

A class message defines two constructors. The first (default constructor) has scope at the package level. This means that only classes within the same package can execute code that looks like this:

 Message msg = new Message(); 

All classes outside the package must call the second constructor.

+1
source share

All Articles