可以在java中使用静态类型参数声明方法吗?如果不是,为什么? [重复]

时间:2021-01-05 22:33:11

This question already has an answer here:

这个问题在这里已有答案:

can a method be declared with static type argument in java ? if no, why?

可以在java中使用静态类型参数声明方法吗?如果不是,为什么?

ex:

class A
{
    void m(static int x)
    {
         System.out.println(x);
    }
}

3 个解决方案

#1


1  

I don't believe this is possible and I cannot think of any valid use case for doing it.

我不相信这是可能的,我不能想到任何有效的用例。

It may make sense to make the method static in order to implement a singleton pattern.

将方法设置为静态以实现单例模式可能是有意义的。

class A
{
    static void m(int x)
    {
         System.out.println(x);
    }
}

Then it could be used without having to instantiate A as follows:

然后可以使用它而无需实例化A,如下所示:

A.m(1);

Alternatively you might want to make x immutable to avoid unexpected behavior. This would be done using "final" as follows:

或者,您可能希望使x immutable以避免意外行为。这将使用“final”完成,如下所示:

class A
{
    void m(final int x)
    {
         System.out.println(x);
    }
}

But making x static would serve no purpose.

但是制作x静态不会有任何意义。

#2


0  

No it won't be allowed (compiler error) and it makes sense as well. The static keyword means that a variable will have only one instance in its scope and that instance is invisible outside that scope. Neither of this makes sense for a function argument.

不,它不会被允许(编译器错误),它也是有道理的。 static关键字表示变量在其范围内只有一个实例,该实例在该范围外是不可见的。这对于函数参数都没有意义。

§6.7.5.3/2: "The only storage-class specifier that shall occur in a parameter declaration is register."

§6.7.5.3/ 2:“在参数声明中应该出现的唯一存储类说明符是寄存器。”

#3


0  

Static members are considered as class level members and gets loaded in memory during class loading, which means it is desirable that it shouldn't have dependency on the class instances.

静态成员被视为类级别成员,并在类加载期间被加载到内存中,这意味着它不应该依赖于类实例。

So your class has member method m which need a parameter to be passed to execute the method body.

所以你的类有成员方法m,需要传递一个参数来执行方法体。

If you declare a static member for it that doesn't make any sense because it doesn't have any existence outside the method, class has no information about it load time which severely violates all the rules stated above.

如果你为它声明一个没有任何意义的静态成员,因为它在方法之外没有任何存在,那么类没有关于它的加载时间的信息,这严重违反了上述所有规则。

#1


1  

I don't believe this is possible and I cannot think of any valid use case for doing it.

我不相信这是可能的,我不能想到任何有效的用例。

It may make sense to make the method static in order to implement a singleton pattern.

将方法设置为静态以实现单例模式可能是有意义的。

class A
{
    static void m(int x)
    {
         System.out.println(x);
    }
}

Then it could be used without having to instantiate A as follows:

然后可以使用它而无需实例化A,如下所示:

A.m(1);

Alternatively you might want to make x immutable to avoid unexpected behavior. This would be done using "final" as follows:

或者,您可能希望使x immutable以避免意外行为。这将使用“final”完成,如下所示:

class A
{
    void m(final int x)
    {
         System.out.println(x);
    }
}

But making x static would serve no purpose.

但是制作x静态不会有任何意义。

#2


0  

No it won't be allowed (compiler error) and it makes sense as well. The static keyword means that a variable will have only one instance in its scope and that instance is invisible outside that scope. Neither of this makes sense for a function argument.

不,它不会被允许(编译器错误),它也是有道理的。 static关键字表示变量在其范围内只有一个实例,该实例在该范围外是不可见的。这对于函数参数都没有意义。

§6.7.5.3/2: "The only storage-class specifier that shall occur in a parameter declaration is register."

§6.7.5.3/ 2:“在参数声明中应该出现的唯一存储类说明符是寄存器。”

#3


0  

Static members are considered as class level members and gets loaded in memory during class loading, which means it is desirable that it shouldn't have dependency on the class instances.

静态成员被视为类级别成员,并在类加载期间被加载到内存中,这意味着它不应该依赖于类实例。

So your class has member method m which need a parameter to be passed to execute the method body.

所以你的类有成员方法m,需要传递一个参数来执行方法体。

If you declare a static member for it that doesn't make any sense because it doesn't have any existence outside the method, class has no information about it load time which severely violates all the rules stated above.

如果你为它声明一个没有任何意义的静态成员,因为它在方法之外没有任何存在,那么类没有关于它的加载时间的信息,这严重违反了上述所有规则。