• 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

Constructors...

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was looking over the SCJP book some more (finally finished chapter 2 lol).

and it said that if a constructor is not provided (none at all) when you call:

Animal animal = new Animal();

A no-arg constructor for the class will automatically be generated.
Likewise, in that no-arg constructor super() and this() will be called
first before any of the instructions in that constructor (Even if the constructor
is not auto-generated).

But, if you call This() inside a constructor, super() is not called.

now my question is, what if the super class doesnt have a no-arg constructor,
is one created automatically for the super class too?

Thanks,

Justin
 
Ranch Hand
Posts: 136
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, every class will have a default constructor predefined unless and until you have created a parameterized constructor inside the class. If you have declared a parameterized constructor inside a class, then you must manually implement the default constructor.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sweet.

Thanks,

Justin
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Balagopal Kannampallil wrote:Yes, every class will have a default constructor predefined unless and until you have created a parameterized constructor inside the class. If you have declared a parameterized constructor inside a class, then you must manually implement the default constructor.

You mean the compiler will create a default constructor if you don't write any constructors in the class. If you want a no-args constructor, you will have to write it yourself.

If you are asking that sort of question, you need to consider whether your design should permit a no-args constructor at all.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so say I do the following:



In both calls to '... new B(); and ... new B(1);'
a call to super() would be put in by the compiler right?
and a no-arg constructor would be created by the compiler
for class A right? At compile time that is....

Thanks for all the help,

Justin Fox
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can confirm that by compiling class A and B and C, then using the javap tool to inspect their bytecode. That will show where the default constructors appear. If you use javap -c you get the interpretation of the bytecode, which looks like assembler, and I think you will find the default constructor is empty.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just don't use javap -c for code of any length; if you have more than 20 lines in the .java file the printout will vanish off the end of the terminal or command window!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Just don't use javap -c for code of any length; if you have more than 20 lines in the .java file the printout will vanish off the end of the terminal or command window!



On Windows, I guess we're talking about -- any other platform has decent terminal scrollback by default. Try cranking CMD.EXE's "Properties | Layout | Screen Buffer Size" up to 2000 or so. That, and selecting "Options | QuickEdit Mode" are both absolutely necessary to make CMD.EXE tolerable.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right; it was on Windows that I had that problem.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sweet,

Thanks for help.

Justin
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic