-1

I am starting use Java generic. I have a Parameter interface and its ParameterImpl concrete class. The T will be a Float, Long, Integer or String.

public interface Parameter<T extends Comparable<T>>  {}  
public class ParameterImpl<T extends Comparable<T>> implements Parameter<T> {}

In other classes, saying public class A and public class B:

  1. class variable of type Parameter can be declared as: Parameter parameter or Parameter<?> parameter but not Parameter <T> parameter.

    //warning, should be parameterized
    Parameter p;
    //no warning
    Parameter<?> p2;
    //error
    Parameter<T> p3;
    //warning, should be parameterized
    Map<String, Parameter> pMap;
    //no warning
    Map<String, Parameter<?>> pMap2;
    //error
    Map<String, Parameter<T>> pMap3;
    

Then should I use the Wildcard everywhere when asked? or just ignore the warning and use the non-parameterized version?

  1. there are three options for the methods:

        public <T extends Comparable<T>> Parameter<T> getParameter2(Map<String, Parameter<T>> map, String key) { return null; }
    
         public Parameter <?> getParameter (Map<String, Parameter<?>> map, String key){ return null; }
    
        public Parameter getParameter3(Map<String, Parameter> map, String key) { return null; }
    

similar as the class variable cases, the last one has warning about the parameterized. Which way I should go?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
5YrsLaterDBA
  • 33,370
  • 43
  • 136
  • 210
  • Use the `?` wildcard only when you _really don't care about the type_ at runtime. If you find yourself using it a lot, go back and rethink your design. – William Price Dec 21 '14 at 23:00

2 Answers2

0

You should declare the concrete type such as:

Parameter<Long> p2 = ...;

Your method will then be:

public Parameter<T> getParameter3(Map<String, Parameter<T>> map, String key) { return null; }

or if you require a more specific version

public Parameter<Long> getParameter3(Map<String, Parameter<Long>> map, String key) { return null; }

The wildcard is used then it could be any type i.e. when you write a very general function or declare a very general parameter. You could also use bounded wildcards which forces you to inherit from e.g. Number such as this:

Paremeter<? extends Number> numberedParameter;
wassgren
  • 18,651
  • 6
  • 63
  • 77
  • public Parameter getParameter3(Map> map, String key) { return null ;} will get compile error. We have to have > after the public key word. Because this method is NOT in a generic class. – 5YrsLaterDBA Dec 21 '14 at 23:36
  • well, there you go - use an appropriate typing and in your case I guess you have already figured it out. Don't use the wildcard `>` unless that is *what you need*. – wassgren Dec 21 '14 at 23:39
0

Your type Parameter is generic.

This

Parameter p;

is using Parameter as a raw type. Avoid using it.

This

//no warning
Parameter<?> p2;

is using Parameter with a type argument of ?, the wildcard. You can assign any value of type Parameter to the variable p2, regardless of its parameterization.

This

//error
Parameter<T> p3;

is attempting to use a type named T as a type argument to Parameter. If such a type doesn't exist or isn't in scope, a compiler error will occur. That's what seems to be happening.

Then should I use the Wildcard everywhere when asked?

No, use an appropriate type argument. This depends on what you want to do.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I think I understand the class variable case now. How about the Map cases? The Parameter in a map is mixed, some are Long and some are Float and others. Then it is the right place for wildcard? I don't want to make Class A or Class B a generic type. – 5YrsLaterDBA Dec 21 '14 at 23:33
  • @5YrsLaterDBA I don't understand your question. Generics are _generic_. You can apply them to the type(s) you need. If you don't care what that type is, you can use a wildcard. If you want to apply restrictions on the types you apply type bounds. – Sotirios Delimanolis Dec 22 '14 at 05:30