• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question on constructors

 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I was doing a mock test on OCA and found a question on constructors. The code I will use for my question:



As it can be observed from the code, the instance variable 'i' gets its default value '0' after the constructor of class 'Sub1' had called the constructor of its super class 'Super1'. I was always convinced that the constructor is the one, who initializes the instance variables by their default values.

The multichoice question on the cunstructors is as follows:

"Which of these statements are true:

<...>
* A constructor must initialize all the member variables of the class.
* A constructor can access non-static members of the class."

i had to choose between two these items. I chose the latter. And it was a right choice. When I looked at the explanation on the former item I got: "All non-final instance variables get default values if not explicitely initialized." Could someone advise, whether it is a good explanation?
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergei Zhylinski wrote:When I looked at the explanation on the former item I got: "All non-final instance variables get default values if not explicitely initialized." Could someone advise, whether it is a good explanation?

It's a correct explanation. You can initialize instance variables using a constructor, but it's not required. In the following code snippet you have 3 constructor: the 1st one initializes no instance variables, the 2nd initializes only firstName and the 3rd one initializes all instance variables. This code snippet compiles without errors.


Things change when you have final instance variables. A final instance variable MUST be initialized using one of these alternatives: an explicit initialization, a constructor or an initializer block. If you don't adhere to this rule, compilation fails. So let's see what happens if we add the final modifier to firstName:

As expected, compilation fails due to an error on line1: this constructor doesn't initialize the final instance variable firstName. If you replace line1 with public Person() { this("Sergei"); }, the code snippet will successfully compile.

Hope it helps!
 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Sergei Zhylinski wrote:When I looked at the explanation on the former item I got: "All non-final instance variables get default values if not explicitely initialized." Could someone advise, whether it is a good explanation?

It's a correct explanation. You can initialize instance variables using a constructor, but it's not required. In the following code snippet you have 3 constructor: the 1st one initializes no instance variables, the 2nd initializes only firstName and the 3rd one initializes all instance variables.


Thanks. But the String instance vars you provided will be implicitly initialized by their default values (nulls). Who initializes these fields then when the first constructor is used?
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergei Zhylinski wrote:Who initializes these fields then when the first constructor is used?

The JVM gives the class & instance variables their default values.
 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Sergei Zhylinski wrote:Who initializes these fields then when the first constructor is used?

The JVM gives the class & instance variables their default values.


Thanks. I should reread the JLS.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic