• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question found online - what is the constructor that initialize x3?

 
francisco de feudis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which constructor initializes the variable x3?



Answers:

    A. Only the default constructor of class X
    B. Only the no-argument constructor of class Y
    C. Only the no-argument constructor of class Z
    D. Only the default constructor of object class


In my opinion the answer is A, because the x3 is an instance variable of X and not of Object class, thus it can be initialized only when X constructor is called.
Is it correct?
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
francisco de feudis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Please QuoteYourSources.

Henry


Hi Herny, it is a question found online here:

http://stackoverflow.com/questions/32969421/which-constructor-initializes-the-variable-x3

Sorry for the mistake.
Thank you for help.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
francisco de feudis wrote:In my opinion the answer is A, because the x3 is an instance variable of X and not of Object class, thus it can be initialized only when X constructor is called.
Is it correct?

Honestly, I don't know! I don't understand the question itself and in my opinion it is a very poor question.

If you put a gun to my head and I must choose an answer, I would opt for option C. Explanation: the no-argument constructor of class Z will first invoke the no-argument constructor of class Y. And then the no-argument constructor of class Y will invoke the default constructor of class X. In this constructor the three instance variables (x1, x2, and x3) are initialized with their default value (although it's very debatable if that's really done by the constructor). This is the 1st initialization of instance variables x1. Then execution goes back to the no-argument constructor of class Y and x1 is initialized to 1 (= 2nd initialization). And finally execution goes back to the no-argument constructor of class Z and x1 is initialized to 3 (= 3rd initialization). So the no-argument constructor of class Z is the only constructor which results in an instance variable (x1) to be initialized 3 times.
I would definitely not opt for answer A, because that constructor "initializes" three instance variables once, which is not what the question is asking for in my opinion. The default constructor of class Object doesn't initialize any instance variables, so that's definitely an incorrect answer. And finally the no-argument constructor of class Y causes initialization of two instance variables (x1 and x2) twice, but again that's not what the question is asking for.

Hope it helps!
Kind regards,
Roel
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this question is pretty vague. It should have been, which constructor causes an initialization of the variable x3?  If I'm not wrong, initialization of field int x to It's default value is not done by any constructor. The Java™ Tutorials
The Java™ Tutorials wrote:It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler.

Another old thread I found Here. Please guide me...
 
Roel De Nijs
Sheriff
Posts: 10662
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
Ganesh Patekar wrote:It should have been, which constructor causes an initialization of the variable x3?

Oh boy, what a facepalm moment! Of course the question meant "variable x3" and not "variable three times". Now I feel so stupid Especially for typing an extensive post which is complete nonsense So now I know it is not a good idea to post a reply without having my morning coffee first

Ganesh Patekar wrote:If I'm not wrong, initialization of field int x to It's default value is not done by any constructor.

You are absolutely spot-on! The correct answer should actually be "None of the above". Because as I already mentioned in my previouse (nonsense) post: it's very debatable if the initialization of instance variable is really done by the constructor. I am pretty sure instance variables get them by default (hence it's called "default values" ) without the interference of any constructor. And that's illustrated in this code snippetOutput: y: 0

In this tiopic you'll find a great discussion about the creation of instance variables and objects. Definitely worth reading!

Hope it helps!
Kind regards,
Roel
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Roel, I've gone through that topic. It was just awesome!
 
Daniel Cox
Ranch Hand
Posts: 147
7
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS provides an example (Example 12.5-1. Evaluation of Instance Creation) which shows that instance fields are initialized to their default values before the constructor is called.
Example 12.5-1. Evaluation of Instance CreationHere, a new instance of ColoredPoint is created. First, space is allocated for the new ColoredPoint, to hold the fields x, y, and color. All these fields are then initialized to their default values (in this case, 0 for each field). Next, the ColoredPoint constructor with no arguments is first invoked.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Cox wrote:The JLS provides an example (Example 12.5-1. Evaluation of Instance Creation) which shows that instance fields are initialized to their default values before the constructor is called.

Have a cow for going through the JLS and providing this code snippet which clearly illustrates that the correct answer should be "None of the above" (as I already mentioned in a previous post).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic