Question

The following two methods compiles fine and do what they stand for.

public int returnArray()[]
{
    int a[]={1 ,2};
    return a;
}

public String[] returnArray(String[] array[])[]
{
    return array;
}

According to this method signature, can't we somehow have a method signature like the following?

public <T>List rerurnList(List<T> list)<T>
{
    return new ArrayList<T>();
}

This method is intended to return a java.util.List of generic type. It does not compile. It must be modified as follows for its successful compilation.

public <T>List<T> rerurnList(List<T> list)
{
    return new ArrayList<T>();
}

Can't we have a method signature like the first cases, in this case?

Was it helpful?

Solution

For some reason Java lets you define arrays like in C, adding the [] modifier after the variable or method name. That, however, is not possible with generics.

Generic type arguments have to be declared right with the type, because they are part of the type descriptor. Arrays should also be declared that way, as they are also part of the type descriptor.

In order to understand why the compiler does not let you write things that way (and why it shouldn't let you write things like in the first examples), we need to break it down to pieces.


public int returnArray()[] { ... }
  • public: Visibility declaration
  • int: Return type, integer
  • returnArray: Method name
  • (): Argument list (empty)
  • []: Whoops! the return type is actually an arrayof what we said before

This is even better:

public String[] returnArray(String[] array[])[]
  • public: Visibility declaration
  • String[]: Return type, an array of strings
  • returnArray: Method name
  • (String[] array[]): Argument list...
    • String[]: Type of the argument, array of strings
    • array: Name of the argument
    • []: Whoops! argument type is actually an array of what we said before
  • []: Whoops again! return type is actually an array of what we said before

Foot note: Don't do this, specify the types only in the types. Instead of String[] array[], use String[][] array.


Now that the array thing syntax is clear, and I hope you understand why it should be wrong, let begin with the generig thing:

public <T> List<T> rerurnList(List<T> list) { ... }
  • public: Visibility declaration
  • <T>: This method uses generic type T
  • List<T>: Return type, a generic List of T
  • rerurnList: Method name
  • (List<T> list): Argument list
    • List<T>: Argument type, generic List of T
    • list: Argument name

OTHER TIPS

Just to answer your question: the syntax you're trying for the method that fails compilation is definitely wrong because of the <T> placed between the parameter list and the method body:

(List<T> list)<T>{

This is simply not valid Java syntax. That's not how you mark a generic method. You already marked the method as generic by putting the type parameter - <P> - between the method access modifier and its return type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top