0

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.

4
  • Have you imported ReportingService? Commented Nov 11, 2009 at 9:11
  • But even in the IDE does not show the method, can you add it manually and compile the code? Commented Nov 11, 2009 at 9:11
  • 1
    looks as you just have auto-complete problem in IDE... try to type ReportingService.getInstance() and see whether you have compilation errors Commented Nov 11, 2009 at 9:13
  • Is that the exact code in the IDE? Commented Nov 11, 2009 at 9:27

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

these two classes are in different packages
So do you really have a classes called MyApp.Services.WebReportingService.WebReportingService and MyApp.Services.ReportingService.ReportingService ??? That seems a bit loopy to me.
yes there are classes. look "MyApp.Services.ReportingService" is package name and "ReportingService" is class in this package. so that when i import this package i am writing "MyApp.Services.ReportingService.ReportingService"
Changed per your comments. Try again.
+1 for the synchronized getInstance()! Also, the singleton needs a declared private constructor (just in case that's the whole code).
1

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)

2 Comments

still unable to access. i tried complete class name in import statement on WebReportingService
try writing the whole package name in the code; something like: myApp.Services.ReportService.ReportService rs = myApp.Services.ReportService.ReportService.getInstance() . Also check the Consumer class, there seems to be an issue with that as well (it doesn't find the Observer class, possibly another import blunder)
0

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?

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.