Results 1 to 3 of 3

Thread: Constructors...

  1. #1
    hawk-dc is offline Member
    Join Date
    Nov 2013
    Posts
    22
    Rep Power
    0

    Default Constructors...

    Hi there! I'm really confused with this stuff. But can anyone please explain WHY in this class there is CONSTRUCTOR: "public Pet(String initialName, int initialAge, double initialWeight)" and then method "public void setPet(String newName, int newAge, double newWeight)" ??? And can I use only one of them, either the above mentioned constructor or method, in this class??

    /**
    Class for basic pet data: name, age, and weight.
    */


    public class Pet
    {
    private String name;
    private int age; //in years
    private double weight;//in pounds

    public Pet()
    {
    name = "No name yet.";
    age = 0;
    weight = 0;
    }

    public Pet(String initialName, int initialAge,
    double initialWeight)
    {
    name = initialName;
    if ((initialAge < 0) || (initialWeight < 0))
    {
    System.out.println("Error: Negative age or weight.");
    System.exit(0);
    }
    else
    {
    age = initialAge;
    weight = initialWeight;
    }
    }

    public void setPet(String newName, int newAge,
    double newWeight)
    {
    name = newName;
    if ((newAge < 0) || (newWeight < 0))
    {
    System.out.println("Error: Negative age or weight.");
    System.exit(0);
    }
    else
    {
    age = newAge;
    weight = newWeight;
    }
    }
    (

  2. #2
    andy-grom is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Re: Constructors...

    Please use code tags for your Java code! This makes it easier for all of us!

    What you got there is a Plain Old Java Object (POJO). You don't have any (business) logic but just an abstraction of something that exists in the real world.
    The reason for overloading the constructor is simple. You can create a pet without knowing anything about it, so you don't pass any parameters. However, if you know the name, age and weight you can use the constructor you are wondering about.
    The advantage of this constructor is that you don't create the object, set the name, set the age, set the weight (in your case this can be done by using just the setPet method; but what if you just want to change the name??) but just create the instance and setting all properties on the fly.

    For sure, you can use both of them as long as they are visible (in this case they are since both are declared public):
    Java Code:
    // An instance with default values which should be reset using setPet for the sake of usefulness
    Pet pet1 = new Pet();
    // An object with useful default value!
    Pet pet2 = new Pet("Hannes", 5, 3);
    Finally, you could / should optimize your code by using the setPet method in the constructor in order to avoid redundant code!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default Re: Constructors...

    A constructor has to have the same name as the class name; the Java language demands it. A method can have any valid name, including the name of the class, but that is confusing as you have noticed. If you want to create a new object X, you have to do: new X( ... ) and if you already have an object X, say "x" and the class also has that confusing method X( ... ), you can do x.X( ... )

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with constructors
    By Modulus in forum New To Java
    Replies: 2
    Last Post: 02-11-2013, 01:12 AM
  2. Constructors
    By zhider in forum New To Java
    Replies: 5
    Last Post: 11-13-2012, 03:43 AM
  3. Constructors
    By EdOBannon in forum Advanced Java
    Replies: 2
    Last Post: 12-15-2011, 11:05 AM
  4. Need help with constructors
    By tpfaff in forum New To Java
    Replies: 10
    Last Post: 10-22-2010, 04:33 AM
  5. Constructors
    By suresh.sa in forum New To Java
    Replies: 5
    Last Post: 10-20-2010, 12:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •