File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes calling this() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Meteor in Action this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling this()" Watch "calling this()" New topic

calling this()

Rajat Sarkar

Joined: Sep 07, 2008
Posts: 18
public abstract class Animal
private int teeth_number;

public Animal(int x)

public Animal()
System.out.println("These do not execute"); // this line does not execute }

public int showTeeth()
return teeth_number;

public abstract void roam();

Why the underlined line does not execute while creating subclasses of Animal?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

It would execute if the subclass invokes the no-argument constructor of the superclass; but if the subclass uses "super(32)" or otherwise calls the constructor with an int argument, the no-argument constructor will not be executed.

[Jess in Action][AskingGoodQuestions]
Melissa Raven

Joined: Dec 30, 2008
Posts: 4
This ran fine for me:

It is sorta covered in the JavaRanch Style Guide.
subject: calling this()