Static common methods in .Net vs Java

I'm not so sure.

In C #, I can do the following.

public class Helper<T> where T : new()
{
    public static T SomeHelperFunction(string strValue)
    {
        return new T();
    }
}

Here, the static method can explicitly use the type passed to the class Helper.

Here is my exact attempt in Java / Android

public class Helper<T> {
    public static T SomeHelperFunction(String strValue){
        //some code
    }
}

Java complains about T used in SomeHelperFunction. Why does .Net allow this, but not Java, or am I missing something?

How can I create a .Net class in Java?

+4
source share
1 answer

You can do it

public class Helper<T> {
public static <T> T SomeHelperFunction(String strValue){
    //some code
}
}

In Java, static general methods need their own generic declaration,

+4
source

Source: https://habr.com/ru/post/1677169/


All Articles