• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Dan's Question on Constructors

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the following question on Dan's mock on constructors:
Suppose that the superclass constructor invocation statement, super, appears explicitly in a subclass constructor. If a compile-time error is to be avoided then the arguments for the superclass constructor invocation statement, super, can not refer to which of the following?
a. Static variables declared in this class or any superclass.
c. Static methods declared in this class or any superclass.
The above were among the valid answers.
Is this possible because all classes in the hierarchy are loaded initially and then the constructors invoked hierarchially ?
My understanding of object initialization is:
1. super or this is invoked
2. static variables and static methods are executed in textual order.
3. instance variables and instance methods are executed in textual order.
4. rest of the constructor is executed.
Is the above sequence correct?
Thanks.
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't find this question, can you post a link to it?
Also: 6-Instance Member Variables can NOT be referenced in a constructor before the call to super() returns. So super(x) where x is an instance variable will give a compile time error.
This I'm sure of.. about static variables, I don't think these 2 answers are correct but maybe there's something Dan knows that's not clear to me, that's why I wanted to see his comment on the question in the answers he offer.
Also: 1-On instantiating an object of a certain type, the static initializers of the super classes are executed first starting with the highest class in the heirarchy, then the static initializer of the class of the object itself, then because of an implicit or explicit super() call the instance initializers and the constructors of the super classes are executed in couples (instance initializer & constructor) then at the end the inistance initializer and constructor of the class of the object itself are executed.
2-If any static variables are declared in a class, they will be initialized during the loading of the class. Instance variables are initialized with the creation of a new object (instance) of the class.
HTH
 
Vijay Albuquerque
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alfred.
Below is the link (Question 3):
http://www.danchisholm.net/nov16/topic/declare/constructors1.html
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vijay Albuquerque:

The above were among the valid answers.



Vijay,
The answers that I provide for that question are as follows:
  • Instance variables declared in this class or any superclass.
  • Instance methods declared in this class or any superclass. The keyword "this".
  • The keyword "super".

  • The remark associated with the question is as follows.

    The superclass constructor invocation statement, super, is processed before the new instance of the class has been created. For that reason, the arguments to the superclass constructor invocation statement can not legally refer to members that have not been created and initialized.


    Please note that question asks

    If a compile-time error is to be avoided then the arguments for the superclass constructor invocation statement, super, can not refer to which of the following?


    The reader is required to select the true statements only. The true statements were the ones that I posted earlier in this response. The reader should assume that any answer that is not listed as "true" is threrefore "false". The two answer options that you have posted were the two answer options that are not among the set of true answers and therefore should be assumed false.
     
    Dan Chisholm
    Ranch Hand
    Posts: 1865
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please see the answer here.
     
    Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
    Garden Master Course kickstarter
    https://coderanch.com/t/754577/Garden-Master-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic