This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 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: 24193

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:

I agree. Here's the link:
subject: calling this()
It's not a secret anymore!