Classes in Java can have as many constructors as you like. Using “overloading” you can create an Object with different parameters. You can have constructors with no parameters, some with, some with partial amounts… What ever you like.

Here’s an example.

/**
 * Created by Josh on 11/05/2016.
 */
public class LoadsOfConstructors {
    
    String name;
    int age;
    String colour;
    
    
    //constructors..
    public LoadsOfConstructors(){
        
    }
    
    public LoadsOfConstructors(String name){
        this.name = name;
    }
    
    public LoadsOfConstructors(int age){
        this.age = age;
    }
    
    public LoadsOfConstructors(String name, int age, String colour){
        this.name = name;
        this.age = age;
        this.colour = colour;
    }
    
    public LoadsOfConstructors(int age, String name, String colour){
        this.age = age;
        this.name = name;
        this.colour = colour;
    }
    
    
    public static void main(String[] args){
        LoadsOfConstructors Josh = new LoadsOfConstructors(13, "Josh", "Blue");
        LoadsOfConstructors EmptyMan = new LoadsOfConstructors();
        LoadsOfConstructors Rock = new LoadsOfConstructors(112409);    
        
    }
    
}

This only real issue that you will run into is if you doing something like this…

    public LoadsOfConstructors(String name){
        this.name = name;
    }

    public LoadsOfConstructors(String colour){
        this.colour = colour;
    }

That’s because Java can’t discern between the two. Both will be passed a String. How does Java know if it’s a name or a colour? It doesn’t.

Until next time, Josh.

Leave a Reply
You May Also Like

GoldAusGang.com Review – Sketchy AF

I was contacted a few days ago, by a guy that wanted…

5 Success Tips for WordPress-based CPA Landing Pages

WordPress is the most popular CMS on the internet and its fantastic…