Java:无法访问静态单例方法

Java:无法访问静态单例方法,java,static,singleton,Java,Static,Singleton,我面临一个问题,我有一个名为“ReportingService”的类,它应该是singleton,并且正在扩展“CommonService” 在我使用的其他类中访问这个类 package MyApp.Services.WebReportingService; @WebMethod(operationName = "registerUDP") public boolean registerUDP( @WebParam(name = "Friendly Name")

我面临一个问题,我有一个名为“ReportingService”的类,它应该是singleton,并且正在扩展“CommonService”

在我使用的其他类中访问这个类

package MyApp.Services.WebReportingService;
@WebMethod(operationName = "registerUDP")
    public boolean registerUDP(
            @WebParam(name = "Friendly Name") String friendlyName,
            @WebParam(name = "Username") String username,
            @WebParam(name = "Password") String password,
            @WebParam(name = "Communication Protocol") CommunicationProtocol communicationProtocol,
            @WebParam(name = "IP Address") String ipAddress,
            @WebParam(name = "Port") int port) {

        Consumer client = new Consumer(friendlyName, username, password, communicationProtocol, ipAddress, port);

ReportingService rs = ReportingService.
        return true;

    }
在“ReportingService rs=ReportingService”中,它没有显示ReportingService类的方法。我还导入了正确的软件包


注意:两个类都在不同的包中。您必须导入源文件顶部的ReportingService类,如下所示:

import MyApp.Services.ReportingService.ReportingService;
编辑:第二个代码段也缺少类声明。如果这是完整的源代码,则无法编译


另一个基于编译器输出的编辑:如果在
使用者
类似乎依赖的类路径上有
xx.xx.Java.Common.DesignPattern.Observer.Observer
类,这也会有所帮助。

您必须在源文件顶部导入ReportingService类,如下所示:

import MyApp.Services.ReportingService.ReportingService;
编辑:第二个代码段也缺少类声明。如果这是完整的源代码,则无法编译


另一个基于编译器输出的编辑:如果在
使用者
类似乎依赖的类路径上有
xx.xx.Java.Common.DesignPattern.obserpattern.Observer
类,它也会有所帮助。

不能确定,但我看到当导入的类与代码一起过时时会发生这种问题


这可能发生在不同的IDE上,没有简单的解决方案:可能清理所有类并重新编译它们?

不能确定,但我看到当导入的类与代码一起过时时会发生这种问题


这可能发生在不同的IDE上,没有简单的解决方案:可能清理所有类并重新编译它们?

我会尝试手动键入getInstance(),正如其他人在他们的评论中所建议的那样。这应该行得通。我认为您的IDE在键入点(.)后不会向您显示该方法的原因可能仅仅是因为以下返回true。它在读书

ReportingService rs = ReportingService.return true;

它认为代码已损坏,因此不会提供代码完成。

我会尝试手动键入getInstance(),正如其他人在评论中所建议的那样。这应该行得通。我认为您的IDE在键入点(.)后不会向您显示该方法的原因可能仅仅是因为以下返回true。它在读书

ReportingService rs = ReportingService.return true;

它认为代码已损坏,因此不会提供代码完成。

我认为您的包名已损坏。您似乎在包的末尾有类名,但类名仅在导入时出现

所以你会:

package myApp.Services;

public class ReportService extends CommonService {/* code goes here */}
但你会包括:

import myApp.Services.ReportService;
WebReportingService也是如此。
语句不应包含类名


编辑:如果您确实想要包
myApp.Services.ReportService
中的
ReportService
,那么您需要导入
myApp.Services.ReportService
(或者,
myApp.Services.ReportService.
,但除非您需要包中的许多类,否则不建议这样做)

我想你的包裹名字坏了。您似乎在包的末尾有类名,但类名仅在导入时出现

所以你会:

package myApp.Services;

public class ReportService extends CommonService {/* code goes here */}
但你会包括:

import myApp.Services.ReportService;
WebReportingService也是如此。
语句不应包含类名

编辑:如果您确实想要包
myApp.Services.ReportService
中的
ReportService
,那么您需要导入
myApp.Services.ReportService
(或者,
myApp.Services.ReportService.
,但除非您需要包中的许多类,否则不建议这样做)更改如下:

package MyApp.Services.ReportingService;  // FIXME - Make package names lowercase!!
                                          // FIXME - Loopy package name

public class ReportingService extends CommonService {

    private static ReportingService instance = null;

    private ReportingService() { }

    public static synchronized ReportingService getInstance() {
        if (instance == null) {
            instance = new ReportingService();
        }
        return instance;   

    }
}

注意:根据在中定义的
消费者
通信协议
类的包,您可能需要导入它们;e、 g.将这些行添加到
程序包
行之前

import some.package.Consumer;
import some.other.package.CommunicationProtocol;
注意2:您当前选择的包名称存在一些严重的样式问题

  • 包名称应全部为小写

  • 包名称的前缀应该是公司、组织或其他组织的(反向)DNS样式标识符;e、 g.
    in.com.yourcompany…
    。注意,有很好的理由形成这个惯例

  • MyApp
    /
    MyApp
    是免费内容,不应使用

  • 服务。reportingservice
    是冗余/冗长的;e、 g.使用
    服务。报告

  • 使用与类和包名称相同的名称是多余的/冗长的

  • 除非
    reporting
    webreporting
    包中有很多类,否则它们可能应该折叠成一个包。很多包含1或2个类的包是没有帮助的

更改如下:

package MyApp.Services.ReportingService;  // FIXME - Make package names lowercase!!
                                          // FIXME - Loopy package name

public class ReportingService extends CommonService {

    private static ReportingService instance = null;

    private ReportingService() { }

    public static synchronized ReportingService getInstance() {
        if (instance == null) {
            instance = new ReportingService();
        }
        return instance;   

    }
}

注意:根据在中定义的
消费者
通信协议
类的包,您可能需要导入它们;e、 g.将这些行添加到
程序包
行之前

import some.package.Consumer;
import some.other.package.CommunicationProtocol;
注意2:您当前选择的包名称存在一些严重的样式问题

  • 包名称应全部为小写

  • 包名称的前缀应该是公司、组织或其他组织的(反向)DNS样式标识符;e、 g.
    in.com.yourcompany…
    。注意,有很好的理由形成这个惯例

  • MyApp
    /
    MyApp
    是免费内容,不应使用

  • 服务。reportingservice
    是冗余/冗长的;e、 g.使用
    服务。报告

  • 使用相同的na