How do I use this keyword in a static method in java?

Can I use a keyword this

inside a static method in Java? I want to display a Toast message inside a static method in my activity class. How should I do it? Thank you.

+3


source to share


6 answers


You can create a static method with one input parameter, which 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();
}

      

+5


source


Now what?



static void thisInStatic(){
    new Object(){
        Object instance = this;
    };
}

      

+6


source


Not. There is nothing to refer to.

+3


source


this is for instance members and the static method will only have access to the static variable

0


source


I believe that "this" represents the object that is calling the method. Static methods are not specifically associated with any particular object. Rather, they are class methods. This is why "this" cannot be used in static methods.

0


source


This refers to the object that will be created. You cannot access such an object from the staitc method. Put your focus on this. Here is a useful link for you http://mindview.net/Books/TIJ4

-1


source







All Articles