Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initialisation confusion

 
Rajat Sarkar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Dog
{
private int teeth_number;
private boolean isMad;
private String name;
private int size;

public Dog(String s,boolean ad,int si)
{
name=s;
isMad=ad;
size=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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All fields have a default value: in the case of numbers 0. This doesn't apply to local variables.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic