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

DT7
  • 1,615
  • 14
  • 26
v37
  • 57
  • 1
  • 6

4 Answers4

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);
}
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • 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?) – v37 Nov 08 '13 at 16:29
  • Thank you very much ! I tried to vote up but says that I need 15 reputation – v37 Nov 08 '13 at 16:49
  • One more question. Why private final List x and not private final ArrayList x ? What's the difference? – v37 Nov 09 '13 at 15:16
  • Because you should always [program to the interface](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface). `List` is the interface and `ArrayList` is the implementation. You should probably read [this](http://docs.oracle.com/javase/tutorial/collections/). – Boris the Spider Nov 09 '13 at 15:26
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.

Bizmarck
  • 2,663
  • 2
  • 33
  • 48
0
Class<Integer> x = new Class<Integer>();

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

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
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.

KKKCoder
  • 903
  • 9
  • 18
  • Yes. The type parameter T will be used as an actual type if you specify it. If you say Class x = new Class(); Then in your class, everywhere T is used, it means Integer. Check this out: http://www.tutorialspoint.com/java/java_generics.htm – KKKCoder Nov 08 '13 at 16:50