Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Getting back to the basics

    It's been 4+ months since I've touched java. Not good! It's time to reawaken what I've learned, starting with the following example of code:
    Java Code:
    package SamPokemon.business;
    import java.text.NumberFormat;
    
    public class Pokemon {
    
    	//instance variables:
    	private String pokemonName;
    	private String pokemonType;
    	private double pokemonPrice;
    	
    	//default constructor:
    	public Pokemon(){
    		this("", "", 0.0);
    	}
    	
    	//overload constructor:
    	public Pokemon(String pokemonName, String pokemonType, double pokemonPrice){
    		this.pokemonName = pokemonName;
    		this.pokemonType = pokemonType;
    		this.pokemonPrice = pokemonPrice;
    	}
    	
    	//set and get methods:
    	public void setPokemonName(String name){
    		pokemonName = name;
    	}
    	public String getPokemonName(){
    		return pokemonName;
    	}
    	/*
    	//Alternate way of making getters and setters for PokemonName:
    	public void setPokemonName(String pokemonName) {
            this.pokemonName = pokemonName;
        }
        public String getCode() {
            return pokemonName;
        }
        */
    	public void setPokemonType(String type){
    		pokemonType = type;
    	}
    	public String getPokemonType(){
    		return pokemonType;
    	}
    	
    	public void setPokemonPrice(double price){
    		pokemonPrice = price;
    	}
    	public double getPokemonPrice(){
    		return pokemonPrice;
    	}
    	
    	public String getPokemonPriceFormatted(){
    		NumberFormat currency = NumberFormat.getCurrencyInstance();
    		String priceFormatted = currency.format(pokemonPrice);
    		return priceFormatted;
    	}
    }
    Why is it so common for the overload constructor to have arguments in the parentheses, but not the default constructor?
    What's the use of having both a default and overload constructor?

  2. #2
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,722
    Rep Power
    6

    Default Re: Getting back to the basics

    It all has to do with sensible defaults. Can you create a useful object with everything set to defaults? Then you can use a (public) default contructor. But in your case, what do you get when you do: new Pokemon();? You get a Pokemon with no name, no type and no price. I presume it's useless like that. So in your specific example, there is no use for a (public) default constructor, because the resulting object is useless.

    Look at StringBuilder for example:
    new StringBuilder() -> creates an empty StringBuilder
    new StringBuilder(String text) -> Creates a StringBuilder with the given text already in it.

    In both cases, the resulting StringBuilder is useful.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,239
    Rep Power
    23

    Default Re: Getting back to the basics

    Note on terms here.
    The default constructor is the one created by the compiler for an object that you haven't provided a constructor for.
    A constructor with no arguments (as in the above code) is not a default constructor.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,722
    Rep Power
    6

    Default Re: Getting back to the basics

    Quote Originally Posted by Tolls View Post
    Note on terms here.
    The default constructor is the one created by the compiler for an object that you haven't provided a constructor for.
    A constructor with no arguments (as in the above code) is not a default constructor.
    Uncle Oracle says:
    You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass.
    (source)

    More specifically, the default constructor is a no-arg constructor, but a user-created no-args constructor is not the default constructor. But that's more nitpicky than I would like. You could argue that these terms can be used interchangably. If someone points at public FoorBar( ) {...} and say "that's the default constructor for FooBar" I understand him/her.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Help with the basics
    By schvuu in forum New To Java
    Replies: 1
    Last Post: 10-05-2013, 02:12 PM
  2. Scriptlet Not Running on JSP after coming back from back button of browser
    By jason.3dmagic in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 06-23-2011, 07:44 AM
  3. Really Basics
    By Taluntain in forum New To Java
    Replies: 16
    Last Post: 10-08-2009, 09:43 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
  •