• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

default constructor??

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following are true about the "default" constructor? Select 2 correct options
(1) It is provided by the compiler only if the class does not define any constructor
(2) It initializes the instance members of the class
(3) It calls the default 'no-args' constructor of the super class
(4) It initializes instance as well as class fields of the class
(5) It is provided by the compiler if the class does not define a 'no- args' constructor

my doubt is why not 2 is correct .....
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srikanth,

If I'm not wrong 1,2,3 is the answer.....right!!!
And I've gone through JLS, I dint find any reason for 2 to be incorrect..

But, can we think of the question like....they have asked for 2 correct answer they havent specified total how many answers are true...

they have asked for 2...give 2 correct answers....

correct me if I'm thinking on wrong lines

Regards,
Mausam
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the mock hasnt specified ..even if sepcified what u will select coz i feel 123 are all correct .....
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have had to take a hard look at the statements...

and came up with ..2,3..

Felt 1..as correct...

except if you dont consider anonymous class..

An anonymous inner class doesnt have a constructor...only instance initializer

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
default constructor-It initializes the instance members of the class



can any one explain me this point ,it may be trivial one but .....
explain with example
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think (1) and (3) are correct about default constructor.

(2) is wrong because default constructor has nothing defined
its body
(4) and (5) are self explanatory.

Please correct me if i am wrong.
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i feel the same..Amrutha
thats why have asked for explanation
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by manogna edintipal:

An anonymous inner class doesnt have a constructor...only instance initializer


Yes it does - only you can't define any.

Consider this case:

There is a constructor, that takes a String. There are even constructors that take an Icon, or a String and an Icon, or any other constructor AbstractAction has.
As for anonymous classes implementing interfaces (new ActionListener() ...), there is also a constructor - a non-arg constructor.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(2) It initializes the instance members of the class
...
my doubt is why not 2 is correct .....



Seems two books I checked agree with you:
From Core Java (J2SE 5.0) , Volume I, Page 128


If you write a class with no constructors whatsoever, then a default constructor is provided for you. This default constructor sets all the instance fields to their default values. So, all numeric data contained in the instance fields would be 0, all Booleans would be false, and all object variables would be set to null.



From K&B 1.4. Page 314 when they discuss constructor chaining they also say:


5. Object constructor completes
6. Animal instance variables are given their explicit values (if any)
7. Animal constructor completes



Where did you get the question from? Do they provide any answer?
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic