开发者

How to hide a public method?

I have a method in my static state machine that is only used once when开发者_开发百科 my application is first fired up. The method needs to be public, but I still want it hidden. Is there a way to use an annotation or something that will hide the method from the rest of the project?


You cannot make a public method hidden (unless you can declare it private). You can however put in a subclass and only let the users of the object know the type of the superclass, that is:

class A {
   //Externally visible members
}

class B extends A {
   //Secret public members
}

Then you instantiate the class B, but only let the type A be known to others...


Once you declare public method it becomes part of your class's contract. You can't hide it because all class users will expect this method to be available.


You could use package level instead of public. That way it can only be called by your application.


If a method is public, it can't be hidden. What you may really be looking for is just a way to restrict access to calling a method. There are other ways to achieve a similar effect.

If there are some things that your state machine does that are "only used once when my application is first fired up" it sounds a lot like those are things that could happen in the constructor. Although it depends on how complex those tasks are, you may not want to do that at construction time.

Since you said your state machine is static, is it also a Singleton? You could maybe use the Singleton Pattern.

public class SimpleStateMachine {

  private static SimpleStateMachine instance = new SimpleStateMachine();

  private SimpleStateMachine() {
      super();
      System.out.println("Welcome to the machine");  // prints 1st
  }

  public static SimpleStateMachine getInstance() {
      return instance;
  }

  public void doUsefulThings() {
      System.out.println("Doing useful things");  // prints 3rd
  }
}

Here's some code for a client of this Singleton:

public class MachineCaller {

    static SimpleStateMachine machine = SimpleStateMachine.getInstance();

    public static void main(String... args) {
        System.out.println("Start at the very beginning");  // prints 2nd
        machine.doUsefulThings();
    }
}

Note that the SimpleStateMachine instance isn't built until the first time your class is accessed. Because it's declared as static in the MachineCaller client, that counts as a "first access" and creates the instance. Keep this tidbit in mind if you definitely want your state machine to perform some of those initialization tasks at the time your application starts up.

So, if you don't want to turn your state machine class into a true singleton... you can use a static initialization block do your one-time tasks the first time the class is accessed. That would look something like this:

public class SimpleStateMachine {

    static {
        System.out.println("First time tasks #1");
        System.out.println("First time tasks #2");
    }

    public SimpleStateMachine() {
        super();
        System.out.println("Welcome to the machine");
    }

    public void doUsefulThings() {
        System.out.println("Doing useful things");
    }
}

While we're at it, since you mentioned that it's a state machine... the Head First Design Patterns book does a nice, easily understandable treatment of the State Pattern. I recommend reading it if you haven't already.


The idiomatic approach to doing this is to use interfaces to limit the visibility of your methods.

For example, say you have the following class:

public class MyClass {
    public void method1() {
        // ...
    }
    public void method2() {
        // ...
    }
}

If you want to limit some parts of the project to only see method1(), then what you do is describe it in an interface, and have the class implement that interface:

public interface Method1Interface {
    public void method1();
}

...

public class MyClass implements Method1Interface {
    public void method1() {
        // ...
    }
    public void method2() {
        // ...
    }
}

Then, you can limit the visibility of the methods by choosing to pass the class around either as a MyClass reference, or as a Method1Interface reference:

public class OtherClass {
    public void otherMethod1(MyClass obj) {
        // can access both obj.method1() and obj.method2()
    }
    public void otherMethod2(Method1Interface obj) {
        // can only access obj.method1(), obj.method2() is hidden.
    }
}

A bonus of this approach is that it can also be easily extended. Say, for example, you now also want to independently control access to method2(). All you need do is create a new Method2Interface along the same lines as Method1Interface, and have MyClass implement it. Then, you can control access to method2() in exactly the same manner as method1().

This is a similar approach to that advocated in @MathiasSchwarz's answer, but is much more flexible:

  • The independent access control described in the preceding paragraph isn't possible with Mathias' technique, due to Java not supporting multiple inheritance.
  • Not requiring an inheritance relationship also allows more flexibility in designing the class hierarchy.
  • The only change required to the original class is to add implements Method1Interface, which means that it is a very low-impact refactor since existing users of MyClass don't have to be changed at all (at least, until the choice is made to change them to use Method1Interface).


An alternative solution: You can make it private and create a invokeHiddenMethod(String methodName, Object ... args) method using reflection.


You said that your public method is used only once when the application is started up.

Perhaps you could leave the method public, but make it do nothing after the first call?


There is a (non-)keyword level package level visibility. Instead of public, protected, or private, you use nothing.

This would make the method or class visible to the class and others in the package, but would give you a certain modicum of privacy. You may want to look at What is the use of package level protection in java?.


Hmm... You want a private method, but want to access it outside?

Try do this with reflection. http://download.oracle.com/javase/tutorial/reflect/index.html


I have seen many Java programmers do something like this:

public static void main(String args[]) {
      new MyClass();
}

So basically they create just one object of the class. If there is a method which should run only once, I guess this approach can achieve that. Your method will be called from inside the constructor. But since I don't know how your app works, what are the constraints, so it is just a thought.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜