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 initialisation confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "initialisation confusion" Watch "initialisation confusion" New topic

initialisation confusion

Rajat Sarkar

Joined: Sep 07, 2008
Posts: 18
class Dog
private int teeth_number;
private boolean isMad;
private String name;
private int size;

public Dog(String s,boolean ad,int si)

public String showName()
return name;

public int showteeth()
return teeth_number;

public class TestDog
public static void main(String[] args)
Dog mydog=new Dog("Rocky",false,50);
System.out.println("name : "+mydog.showName()+" teeth number : "+mydog.showteeth());

In the Dog's constructor we don't initialise teeth_number.Then how we get the value 0 for it when we create Dog object?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45306
All fields have a default value: in the case of numbers 0. This doesn't apply to local variables.
I agree. Here's the link:
subject: initialisation confusion
It's not a secret anymore!