简体   繁体   中英

Call a non static void from another class

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?

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.

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);

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().

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM