相关疑难解决方法(0)

为什么不通过实例调用静态方法为Java编译器的错误?

我相信你们都知道我的意思 - 代码如下:

Thread thread = new Thread();
int activeCount = thread.activeCount();
Run Code Online (Sandbox Code Playgroud)

引发编译器警告.为什么不是错误?

编辑:

要明确:问题与Threads无关.我意识到在讨论这个问题时经常给出Thread示例,因为它们可能真的搞砸了它们.但问题确实是这样的使用总是无稽之谈,你不能(胜任地)写出这样的电话并且意味着它.这种类型的方法调用的任何例子都是barmy.这是另一个:

String hello = "hello";
String number123AsString = hello.valueOf(123);
Run Code Online (Sandbox Code Playgroud)

这使得它看起来好像每个String实例都带有"String valueOf(int i)"方法.

java methods static

75
推荐指数
4
解决办法
3万
查看次数

为什么在从默认接口方法调用静态接口方法时不能使用它?

为什么在接口中从默认接口方法调用静态接口方法我不能使用this.staticInterfaceMethod()而在常规类中从实例方法调用静态类方法是完全有效的 this.staticClassMethod()(虽然它是不好的风格)?

同时,this在接口的默认方法中使用是完全有效的 - 我可以合法地执行以下操作:

interface I {   
    int MY_CONST = 7;   
    static void st_f() {}       
    default void f1() {}

    default void f_demo() {
        this.f1();                  // fine!
        int locvar = this.MY_CONST; // also fine! 

        this.st_f(); // c.ERR:  static method of interface I can only be accessed as I.st_f 
    }
}
Run Code Online (Sandbox Code Playgroud)

java interface

5
推荐指数
1
解决办法
1266
查看次数

标签 统计

java ×2

interface ×1

methods ×1

static ×1