Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Make Java subsystem a singleton

Posted on 2003-03-09
9
Medium Priority
?
232 Views
Last Modified: 2008-02-26
I have 2 subsystems that needed to be singleton. Both subsystems have 1 interface class and 1 discrete class. The problem is java does not allow to have static method in class that implement an inteface. So I cannot have a code like this:

public static MySubSystem getInstance()

that return the singleton object.

Is there any solution for the problem?
0
Comment
Question by:vairoj
  • 5
  • 2
  • 2
9 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8097860
 AFAIK what you are trying to do is perfectly legal. You can have as many static methods/variables as you want in a subclass of an interface. Do you get any error messages when you try to compile you application?
0
 

Author Comment

by:vairoj
ID: 8100234
Ofcourse, the error msg is:

modifier static not allowed here

Because we have to access the subsystem through interface. So the static getInstance() has to be declared on the interface class, too. That's where it generate error, which is understandable because interface class is an abstraction.
0
 
LVL 1

Expert Comment

by:gwang77
ID: 8100247
It can have static method in class that implement an interface, you can check your code first. the follow code can run at my place:

public class Test
{
    public static void main(String[] args) throws Exception
    {
        try
        {
            B.instance().f();
        } catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Failed.");
        }
    }
}
interface A
{
    public void f();
}

class B implements A
{
    public static B b;

    public static B instance()
    {
        if(b == null)
        {
            b = new B();
        }
        return b;
    }

    public void f()
    {
        System.out.println("aaa");
    }
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vairoj
ID: 8100373
Ofcourse, the error msg is:

modifier static not allowed here

Because we have to access the subsystem through interface. So the static getInstance() has to be declared on the interface class, too. That's where it generate error, which is understandable because interface class is an abstraction.
0
 
LVL 1

Accepted Solution

by:
gwang77 earned 300 total points
ID: 8100449
I suggest to change another disign partten to do that.
such as create a SubSystemFactory class to create SubSystem instance, at SubSystemFactory you can let them be singleton instance.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8101773
 You don't have to have the static declaration in the interface. Just declare it in the subclass. If you realyl need to have it in the superclass why don't you use an abstract class?
0
 

Author Comment

by:vairoj
ID: 8101778
Yes, that is a solution that I had in mind. But I try to figure out other solutions (if there is) and choose the best one. But maybe like you said, maybe I have to go for this way.
0
 

Author Comment

by:vairoj
ID: 8101792
Sorry, as I didn't see the last comment when I posted, maybe my last comment looks ambiguous.

I have to make a static declaration in the inteface because I need a static method getInstance() for the subsystem. If I only declare the static method in the subclass, I won't be able to access the method through interface. If I declare the method in the interface as non-static and declare the method in subclass as static, it'll get compile-error.

The abstract class won't work because this is a subsystem, I just need an interface for the subsystem and let clients access the subsystem through interface.
0
 

Author Comment

by:vairoj
ID: 8150260
Because there is no more comment, I'd give points to gwang77, while this is not the answer I was looking for (I already has this solution in my mind as an alternative) but it's the only answer until now ;-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question