Hi guys,

Just a quick question, I'm working on a general stack class for use with my card game and rather than creating an overloaded constructor for every single object and primitive type I'm wondering is there a way to supply the data type when invoking a basic constructor?

Basically what I'm asking is instead of

public class Stack
    public Stack(String any, int size)
        String[] stackOfStrings = new String[size];

    public Stack{int any, int size)
        int[] stackOfInts = new int[size];
    //etcetera through all primitive and object types

Is there a quick way to create a constructor, or any method for that matter, where the type of data structure created is decided when the method is called? I know I could have the constructor take a string and have a number of cases for each word so that I could call Stack("int") and so forth. I'm just wondering if there's any nice tricks out there for cutting down on having to write 10 or so overloaded constructors, after all I'm told programmers hate doing anything in 2 steps where half a step will do :)

8 Years
Discussion Span
Last Post by comSysStudent

How would the compiler handle that? It needs to decide how to connect the caller to the callee at compile time.
You could simulate that functionality by passing an array of Objects to one method and have that method sort out what to do based on the contents of the array.
Very poor coding technique. Subject to many errors.
By having multiple overloaded methods, the user of the method can call it with whatever type of data he/she wants and the compiler finds the correct method to use based on the method's signature(arg types).


Well that's why I'm asking, I'm the noob here :) If it can't be done fair enough I was just thinking there might be a class out there in the api or a method of coding where you can define a variable to refer to a primitive data type, thanks for the quick reply though I guess I'll just stick to good old copy/paste ;)


Is there a quick way to create a constructor, or any method for that matter, where the type of data structure created is decided when the method is called

Yes, the new Java collections API is full of examples of the same. Pick any class from the collections API and look at its source to get a feel of how things are done. Read a bit about generics and you are good to go.

Votes + Comments
concise, to the point, excellent links

Do you know about generics? They're a framework for doing what I think you're talking about. Using the standard library implementation of Stack, this line:

Stack <Card> cards = new Stack<Card>();

will create a Stack of Card objects - if it's not a Card, it won't go on the Stack. If you have GreetingCard and BusinesCard that extend Card, they'll be allowed.

Is that what you're after?

Edited by jon.kiparsky: n/a


Yes that's pretty much it, thanks to you both for further help after I'd marked the thread solved.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.