?
Solved

Make Java subsystem a singleton

Posted on 2003-03-09
9
Medium Priority
?
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 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