• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors...

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • 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
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Sweet.

Thanks,

Justin
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • 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!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Sweet,

Thanks for help.

Justin
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic