0

trying to learn java generic feature but while using it i am getting a warning and unable to understand how i can solve it,though the program in itself is running fine.

i have created a class with following signature

public class MyClass<T> {

    public T demo(String string,Class<T> type){
        // some work 
    }

}

now in my other class i am declaring instance of this class as follow

private MyClass myClass;

and than i am trying to call this method from some places like

(ClassB)myClass.demo("hello",ClassB.class);
(ClassC)myClass.demo("hello",ClassC.class);

The program is working fine but i am finding this warning in eclipse.

Type safety: The method demo(String, Class) belongs to the raw type MyClass. References to generic 
 type MyClass<T> should be parameterized

can any one help me to understand how i can handle this warning?

update

i am using spring to inject myClass instance so can't use new operator here

1
  • I guess you should specify the template argument, like private MyClass<int> myClass; when declaring the instance. Commented Dec 3, 2011 at 15:56

5 Answers 5

2

Try this instead

public class MyClass {

public <T> T demo(String json,Class<T> type){
    // some work  
}

}
Sign up to request clarification or add additional context in comments.

2 Comments

can you expkain what does this mean it will help me to understand
Check out docs.oracle.com/javase/tutorial/java/generics/genmethods.html If you only want the generic on a particular method you declare it with <> before the return type
1

Because of the way you declared your class, Java expects that you give it a type parameter when declaring/initializing a class of that type. So in this case you'd need to do:

private MyClass<ClassB> myclass = new MyClass<ClassB>();

Comments

1

What you try to achieve by calling

(ClassB)myClass.demo("hello",ClassB.class);
(ClassC)myClass.demo("hello",ClassC.class);

can be achieved if you remove the <T> from your class. So replace

public class MyClass<T>

by

public class MyClass

and changing the signature of the method to

public <T> T demo(String json,Class<T> type)

I would recommend reading the Generics tutorial, but this part of the tutorial shows how and explains why

Comments

1

You need to declare your var as:

private MyClass<ClassB> myClassClassB;

---- update

If you do not know the type of the generic initially, you can use this:

private MyClass<? extends Class> myClass;

That will get rid of the warning, though I think you are not fully understanding generics. What is it you are trying to accomplish with this? You are having spring inject your myClass variable with an implemention of MyClass, but the Generics dont much help because generics only mean anything at compile time, and Dependency Injection happens at run time...

1 Comment

my mistake i forgot to mention i am using spring so using a single refrence which spring DI is injecting
0

Basically, your class definition public class MyClass<T> tells the compiler that MyClass uses a generic type. But when you create an instance of it in private MyClass<SomeType> myClass; you did not provide the type to use. Use:

private MyClass<ClassB> myClass;

or

private MyClass<ClassC> myClass;

instead of:

private MyClass myClass;

1 Comment

my mistake i forgot to mention i am using spring so using a single refrence which spring DI is injecting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.