1

For example I have a class which starts like this

public class Class<T> {
    private ArrayList<Integer> x = new ArrayList<Integer>();

In main when I do

public class Main {
    public static void main(String args[]) {
        Class x = new Class();

All elements in x should be of Integer type. How can I do something like Class x = new Integer/String ... Class(); So I can specify the type of the objects in x

2
  • 3
    Don't call a class Class this is just confusing. Commented Nov 8, 2013 at 16:16
  • 1
    @BoristheSpider It is also not a good practice. Commented Nov 8, 2013 at 16:18

4 Answers 4

1

You mean something like this?

public class MyClass<T>{
    private final List<T> x = new ArrayList<T>();

Now your List is of the generic type given to MyClass.

EDIT

OP in comment - if I want to make a fonction which adds elements to x.

In order to do that you need to create a method (rather than function) that takes an item of type T and adds it to x

public void addThing(final T thing) {
    x.add(thing);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks I think thats what I need. But if I want to make a fonction which adds elements to x. How can I call it? x.add(T object?)
Thank you very much ! I tried to vote up but says that I need 15 reputation
One more question. Why private final List<T> x and not private final ArrayList<T> x ? What's the difference?
Because you should always program to the interface. List is the interface and ArrayList is the implementation. You should probably read this.
1

Use Generics.

public class YourClass<T>{
    private ArrayList<T> x = new ArrayList<T>();

Then in Main

public class Main{
    public static void main(String args[]){
        YourClass<Integer> x = new YourClass<Integer>();
}

or

YourClass<String> x = new YourClass<String>();

YourClass<T> could be then an Integer, String, or whatever.

1 Comment

This is what you want. +1 for saying that it's generics that the OP is looking for.
0
Class<Integer> x = new Class<Integer>();

Don't use names for classes that are already defined in java.lang package.

1 Comment

OP Probably also needs to change to private ArrayList<T> x = new ArrayList<T>();
0

I think you are confused by Polymorphism and Generics.

What you want:

Class x = new Integer();

is polymorphism. This statement will only be valid if Class is a base class of Integer, which is clearly not the case.

Parent x = new Child();

will work if

class Child extends Parent {
    ....
}

The class you have written:

public class Class<T> {
    private ArrayList<Integer> x = new ArrayList<Integer>();
    ....
}

is a generic class but you do not use the type parameter. This is not a problem though, your class will still work. When you say:

Class x = new Class();

The type T will simply be Object. But since you do not use this type parameter, it does not have any effect.

To summarize, your statement of:

Class x = new Integer();

will never work as Integer does not have a "is a" relationship with Class. Generics is not used for this purpose.

1 Comment

Yes. The type parameter T will be used as an actual type if you specify it. If you say Class<Integer> x = new Class<Integer>(); Then in your class, everywhere T is used, it means Integer. Check this out: tutorialspoint.com/java/java_generics.htm

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.