Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use this keyword in a static method in java?

Is there any way to use this keyword inside a static method in Java? I want to display a Toast message inside a static method in my activity class. How do I do that? Thanks.

like image 667
Santhosh Avatar asked Apr 26 '26 10:04

Santhosh


2 Answers

Now what?

static void thisInStatic(){
    new Object(){
        Object instance = this;
    };
}
like image 189
Java42 Avatar answered Apr 28 '26 23:04

Java42


You can create a static method with one input parameter that is the Class you need to use.

For example:

public static void showMyTouch(MyActivity act, String message){
   Toast.makeText(act, message, Toast.LENGTH_LONG).show();
}
like image 44
NamLe Avatar answered Apr 29 '26 00:04

NamLe