Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about Java class constructor

Tags:

java

Can somebody tell me what does this mean? I'm going trough Java book and I've encontered this example :

public class Message {

    Message(){}

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

What does Message(){} Mean ?

like image 372
Gandalf StormCrow Avatar asked Mar 09 '10 15:03

Gandalf StormCrow


Video Answer


2 Answers

It's a package private empty constructor taking no arguments.

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

It's worthwhile to know it will not initialize the text field, which will therefore hold the default null value.

like image 52
Tim Avatar answered Oct 06 '22 00:10

Tim


just like

Message()
{
}

but using less lines.

the access level for it is the (default) package access level meaning only classes within the same package can instantiate this object using this constructor.

like image 41
Omry Yadan Avatar answered Oct 06 '22 00:10

Omry Yadan