0

I am trying to end an never ending circle. I need to call a void that is not static from another class. The reason that I do not make it static is that some things are very hard to make static. (Everything inside a static void needs to be static).

I am trapped in a circle where I need to call a non static void from another class. I can not make it static because it some code do not like to be passed.

Till now I solved it sort of by a handler:

public static void change(){
//This is called to change a boolean
start=true;}

private void startDone(){
int timeBetweenChecks = 50;
final Handler h = new Handler();
h.postDelayed(new Runnable(){
    public void run(){
        if (start==false){
           startDone();

        } else{  
            //Do something          
        }          
        }
    }
}, timeBetweenChecks);

};

The problem with this is that I have to run a handler that is checking if something has changed pretty often(In my case).

Is there any way of calling the non static startDone() directly?

Magakahn
  • 498
  • 9
  • 31
  • If it's a non-static class, I'm assuming you just need to create an instance of the class containing startDone() in order to call that method. But this is coming from someone who hasn't done any Android development. – MadHenchbot Nov 20 '12 at 21:16
  • I think you might start with the definition of static: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – ajpolt Nov 20 '12 at 21:23

5 Answers5

6

If you are asking if there is a way to call a non-static method of a class without instantiating an object of that class, then no.

If I don't own a dog, I cannot tell my dog to sit.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • Do you know a better walkaround then a handler? – Magakahn Nov 20 '12 at 21:26
  • 1
    Assuming this is indeed for an Android application, then a few other users have suggested some great solutions. I would try one of those (newbyca's Broadcast method looks promising). Again, depending on what exactly you are trying to accomplish, an [Observer](http://en.wikipedia.org/wiki/Observer_pattern) pattern (often seen in Java as [listeners](http://stackoverflow.com/questions/2975935/java-correct-pattern-for-implementing-listeners) of varying types) may also be appropriate. – Bryan Herbst Nov 20 '12 at 21:29
  • +1 for good example, but I accept newbycas answear since he also had a solution for my problem. – Magakahn Nov 21 '12 at 14:40
3

The answer to your question is: No, you cannot call a non-static method from a static method without an instance of the class containing the non-static method.

To solve your problem: maybe the best way would be to broadcast an intent from change().

Something like:

public static void change(Context c){
    start=true;
    c.sendBroadcast(new Intent("CHANGE_HAS_BEEN_CALLED"));
}

Then in the non-static code of your activity you can register a receiver like this:

IntentFilter filter = new IntentFilter();
filter.addAction("CHANGE_HAS_BEEN_CALLED");
registerReceiver(new BroadcastReceiver() {
    @Override public void onReceive(Context context, Intent intent) {
       if (start==false){
           startDone();

        } else{  
            //Do something          
        }
    }
}, filter);
newbyca
  • 1,523
  • 2
  • 13
  • 25
  • It is possible to send the Broadcast from another class, right? – Magakahn Nov 20 '12 at 21:28
  • It's possible to send the broadcast from any class, you just need an instance of Context. – newbyca Nov 20 '12 at 21:28
  • So it will work sending it, and when it recieves it will call the onRecieve? (As long as I define the BroadCast reciever in the manifest.) – Magakahn Nov 20 '12 at 21:30
  • That's the idea, yes! But in this case you will not need to declare the Intent in your manifest. You only need that for Intents you wish other applications to know about. And you only need to declare the receiver in your manifest if it's a separate class. You can also implement the receiver in an ordinary Activity and avoid the extra configuration (which is the typical approach). – newbyca Nov 20 '12 at 21:39
0

By definition, if startDone() is non-static, then it makes no sense to call it unless you've instantiated the class that contains it. A non-static method is an instance method, which means it can return a different result for every object of its enclosing type.

I think what you want is a class that only contains startDone(). You want to instantiate the class once for your entire application, and then be able to call startDone().

Suppose the class is called ItsDone. Instantiate it as a singleton, then return the singleton when you do a "new", and call startDone().

Joe Malin
  • 8,621
  • 1
  • 23
  • 18
0

a handler that is checking if something has changed pretty often (In my case).

Sounds like callback to me. You pass a piece of code to that "something", and this piece of code is executed by "something" whenever its state changes. If you have control over this "something", it's very easy to implement such behavior, if you don't (if "something" is a part of some library), it probably has this behavior implemented (of course, if it is well-designed).

Anyway, checking the state "something" by querying it every, say, 50 ms is not the way to go.

Andrii Chernenko
  • 9,873
  • 7
  • 71
  • 89
0

The accepted answear for this question is a better solution then the solutions that are already sugested. Hope this will help anybody googling.

Community
  • 1
  • 1
Magakahn
  • 498
  • 9
  • 31