aspose file tools*
The moose likes Beginning Java and the fly likes Constructors... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructors..." Watch "Constructors..." New topic
Author

Constructors...

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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


You down with OOP? Yeah you know me!
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

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.


SCJP 5.0 and now fighting with ExtJS
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
Sweet.

Thanks,

Justin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Jan 24, 2006
Posts: 802
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
That's right; it was on Windows that I had that problem.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
Sweet,

Thanks for help.

Justin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors...