English 中文(简体)
Java: unable to access static singleton method
原标题:

i am facing one problem.i have a class named "ReportingService" and it should be singleton and is is extending "CommonService".

package MyApp.Services.ReportingService;

public class ReportingService extends CommonService {

    private static ReportingService instance = null;

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

    }
}

and to access this class in other class i am using

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;

    }

in "ReportingService rs = ReportingService." it is not showing me getInstance() method of ReportingService class. I also imported correct packages.

NOTE: both classes are in different packages.

最佳回答

I think your package names are broken. You seem to have the name of the class at the end of the package - but the name of the class only appears when you import.

So you would have :

package myApp.Services;

public class ReportService extends CommonService {/* code goes here */}

but then you would include:

import myApp.Services.ReportService;

Same goes for the WebReportingService. The package statement should not include the class name

Edit: if you actually want ReportService in package myApp.Services.ReportService, then you need to import myApp.Services.ReportService.ReportService (or, alternatively, myApp.Services.ReportService.*, but this is not recommended unless you need many classes from the package)

问题回答

Change as follows:

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;   

    }
}

and

import MyApp.Services.ReportingService.ReportingService; 

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

public class 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.getInstance();
        return true;
    }
}

Note: depending on which packages the Consumer and CommunicationProtocol classes are defined in, you may need to import them; e.g. add these lines before the package line.

import some.package.Consumer;
import some.other.package.CommunicationProtocol;

Note 2: your current choice of package names has some seriously style issues.

  • package names should be all lowercase

  • the prefix of your package names should be the (reversed) DNS-style identifier for your company, organization, whatever; e.g. in.com.yourcompany.... Note, there are very good reasons form this convention!!

  • MyApp / myapp is content free and shouldn t be used

  • services.reportingservice is redundant / verbose; e.g. use services.reporting instead

  • using the same name as a class and package name is redundant / verbose

  • unless there are lots of classes in the reporting and webreporting packages, they probably should be folded into one package. Lots of packages with 1 or 2 classes in them is not helpful.

Can t be sure, but I ve seen this kind of problem happening when the imported classes are outdated with the code.

This can happen on different IDEs, there s no straightforward solution: Maybe cleaning all classes and compiling them again?

I would try typing getInstance() by hand, as others have suggested in their comments. That should work. The reason I believe your IDE will not show you the method after typing the dot (.) could be simply because of the following return true. It s reading

ReportingService rs = ReportingService.return true;

which it considers broken code and therefore will not provide code-completion.

You will have to import the ReportingService class at the top of your source file, like so:

import MyApp.Services.ReportingService.ReportingService;

Edit: The second code snippet is also lacking a class declaration. If this is the full source, it cannot compile.

Another edit based on compiler output: It would also help if you had the xx.xx.Java.Common.DesignPattern.ObserverPattern.Observer class on the classpath that the Consumerclass seems to depend on.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签