Abhinav
Abhinav

Reputation: 71

Java - Default constructors concept

If implicit constructors automatically initializes all variables to their default values , then why does Java gives compile time errors like " reference variable not initialized" ?

Upvotes: 1

Views: 86

Answers (2)

Dhaval Kapil
Dhaval Kapil

Reputation: 385

Let me clarify variable initialisation:

All the instance variable having primitive data types are initialized to 0, '\0' and false

Whereas all the others are initialized to null.

The local variables are not initialized and will generate a compile time error.

Upvotes: 1

duffymo
duffymo

Reputation: 308743

All reference types in a class will be initialized as null if you do not set them to something sensible. I'm not aware of compiler errors for that case.

Upvotes: 0

Related Questions