Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: What difference does an additional constructor make to your program?

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default What difference does an additional constructor make to your program?

    Hi, I'm just wondering what benefit a parameterized constructor (other than giving the option of overloading the default constructor)? If I had a default constructor along with getters and setters, what would be the point of also having a parameterized constructor?

    package business;
     
    import java.text.NumberFormat;
     
    public class Pokemon {
     
    	//the instance variables
    	private String name;
    	private String type;
    	private double price;
     
    	//the default constructor
    	public Pokemon(){
    		name = "";
    		type = "";
    		price = 0;		
    	}
     
    	//parameterized constructor
    	public Pokemon(String name, String type, double price){
    		this.name = name;
    		this.type = type;
    		this.price = price;
    	}
     
    	//getters and setters
    	public void setName(String name){
    		this.name = name;
    	}
    	/*
    	//alternative way to declare setter
    	public void setName(String theName){
    		name = theName;
    	}*/
    	public String getName(){
    		return name;
    	}
     
    	public void setType(String type){
    		this.type = type;
    	}
    	public String getType(){
    		return type;
    	}
     
    	public void setPrice(double price){
    		this.price = price;
    	}
    	public double getPrice(){
    		return price;
    	}
     
    	//a custom method for the price variable
    	public String getPriceFormatted(){
    		NumberFormat currency = NumberFormat.getCurrencyInstance();
    		String priceFormatted = currency.format(price);
    		return priceFormatted;
    	}
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What difference does an additional constructor make to your program?

    One benefit would be to make the class immutable. Once it was created via a constructor, its contents would not be changeable via setter methods.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: What difference does an additional constructor make to your program?

    Another reason is to permit easily using default values for one or more parameters.

    public class Square {
         private Color color;
         private int size;
         private static int SIZE = 10;
         private static Color COLOR = Color.RED;
     
        public Square() {
            this(COLOR, SIZE);
        }
     
        public  Square(int size) {
            this(COLOR, size);
        }
         public Square(Color color) {
              this(color, SIZE);
        }
     
        pubic  Square(Color color, int size) { // this constructor will be called either directly or
             this.color = color;                      // by the previous three constructors.
             this.size = size;
        }
    }

    Regards,
    Jim

  4. #4
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: What difference does an additional constructor make to your program?

    Quote Originally Posted by Norm View Post
    One benefit would be to make the class immutable. Once it was created via a constructor, its contents would not be changeable via setter methods.
    So does that mean if I have a parameterized constructor, I don't need getters and setters?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What difference does an additional constructor make to your program?

    An immutable class would not have setters because it is not allowed to change the contents of an immutable class. getters would be allowed. See the String class for an example.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: What difference does an additional constructor make to your program?

    One other observation. Using getters and setters hide the implementation details of how the fields are stored. If you allow them to be set or retrieved directly, you can no longer change that in future releases because you could break existing code. So it's best to make all fields private and use getters and setters for accessing them. That does not negate the need for one or more constructors to aid in creating an instance.

    Regards,
    Jim

  7. #7
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: What difference does an additional constructor make to your program?

    Quote Originally Posted by jim829 View Post
    That does not negate the need for one or more constructors to aid in creating an instance.
    I'm sorry, but I still don't understand the need to create another constructor to overload a default constructor if you have getters and setters. And just to be clear, the reason I want to understand this is just in case it comes up in a job interview.

  8. #8
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: What difference does an additional constructor make to your program?

    Did you see the examples I provided? In the examples I gave it is primarily to make things more concise. If you had to always invoke multiple setters to set say 5 values it's cumbersome and verbose. And as Norm said you can't use a setter in an immutable class. In that case the constructor allows you to provide set the values once. You can't change them. If you used a setter to set it once, you could keep changing it. Then the class would not be immutable.

    Have you ever used classes from the Java API which have multiple constructors? Take a look at this AffineTransform.html

    Would you really want to set all those fields with a series of setters.

    So you really need both together and separately to write good that meets varying requirements.

    Regards,
    Jim

  9. #9
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: What difference does an additional constructor make to your program?

    Quote Originally Posted by jim829 View Post
    In that case the constructor allows you to provide set the values once. You can't change them. If you used a setter to set it once, you could keep changing it. Then the class would not be immutable.
    Thank you Jim, that makes sense

Similar Threads

  1. Replies: 8
    Last Post: October 8th, 2014, 03:37 PM
  2. Replies: 2
    Last Post: January 9th, 2014, 03:31 PM
  3. Adding additional $10?
    By gotdatdough in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 3rd, 2013, 05:45 PM
  4. Replies: 3
    Last Post: March 4th, 2013, 10:51 AM
  5. Design of class named Fan to represent a Fan
    By qaromi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 7th, 2009, 02:28 PM