Java方法定义问题

时间:2012-09-27 11:09:44

标签: java methods

该方法定义如下:

public static void checkNotNull<T extends Throwable>(Object value, String name, Class<T> exceptionClass) 
{

}

然后编译器抛出一些错误:

**此行有多个标记

- void is an invalid type for the variable checkNotNull
- Duplicate field ValidationUtility.value
- Syntax error on token ">", Identifier expected after this token
- The type Class is not generic; it cannot be parameterized with 
 arguments <T>
- The type Class is not visible
- Duplicate field ValidationUtility.exceptionClass
- T cannot be resolved to a type
- Syntax error, insert ";" to complete FieldDeclaration
- Duplicate field ValidationUtility.String**

有什么不对吗?请帮帮我,谢谢。

3 个答案:

答案 0 :(得分:1)

void checkNotNull<T extends Throwable>(...,Class<T> exceptionClass)语法错误。它应该是<T extends Throwable>void checkNotNull(...,Class<T> exceptionClass)

public static <T extends Throwable> void checkNotNull(Object value, String name, 
                                                      Class<T> exceptionClass) {}

答案 1 :(得分:0)

在void之前声明<T extends Throwable>

public static <T extends Throwable> void checkNotNull(Object value, String name,
Class<T> exceptionClass) {}

您可以参考Generics Tutorial

JLS#8.4. Method Declarations

MethodHeader:
     MethodModifiersopt TypeParametersopt Result MethodDeclarator Throwsopt

答案 2 :(得分:0)

public static <T extends Throwable> void checkNotNull(Object value, String name, Class<T> exceptionClass) 
{

}