• 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

About constructor

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
Do the following two statements are both correct ?
The default constructor invokes the no-parameter constructor of the superclass.
The default constructor initializes the instance variables declared in the class.
Thanks in advance!
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
According to me..
The default constructor invokes the no-parameter constructor of the superclass.
TRUE
The default constructor initializes the instance variables declared in the class.
FALSE
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cathy Song:
Hi,
According to me..
The default constructor invokes the no-parameter constructor of the superclass.
TRUE
The default constructor initializes the instance variables declared in the class.
FALSE


I know the first statement is correct because the default construtor will call super() method (no-parameter constructor of the superclass).
But why default constructor doesn't initializes the instance variables ? Then when does thoes instance variable be initialized ?
Thanks,
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jack,


But why default constructor doesn't initializes the instance variables ? Then when does thoes instance variable be initialized ?


Consider the following class:

NOTE: Ofcourse the constructor will not create the default constructor if I create my own. But I wrote out the default constructor in the example above to show you where the variable initilization may take place.
[ November 10, 2003: Message edited by: Cathy Song ]
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jack,


But why default constructor doesn't initializes the instance variables ? Then when does thoes instance variable be initialized ?


To answer this directly:
The instance variables may be initialized in the constrcutors / initializer blocks implemented by the programmer.
I hope this helps.
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic