Patrick
Patrick

Reputation: 313

call static void

How do I call my function?

public static void dial(Activity call) 
{ 
   Intent intent = new Intent(Intent.ACTION_DIAL); 
   call.startActivity(intent); 
} 

Obviously not with:

dial(); /*Something should be within the brackets*/

Upvotes: 0

Views: 737

Answers (4)

Emmanuel
Emmanuel

Reputation: 17051

You can't pass null. You have to send a context object.

Where is your function located? If it's inside an Activity or the such, simply pass "this" as the parameter.

If it's inside an BroadcastListener, or a Service, just change the parameter to Context and pass "this".

Upvotes: 0

Philipp Wendt
Philipp Wendt

Reputation: 2598

What exaclty is the Problem?

If you've got a class like

public class Test {
    public void nonStaticFct() {
        staticFct();
    }

    public static void staticFct() {
        //do something
    }
}

Works perfectly (even if you should call static functions always by Classname.FctName (Test.staticFct())

I guess the problem here is the missing argument.

[Edit] Obviously I am wrong, according to the Java Code Conventions you may use a Classmethod by simply calling it, without using the classname (even if it seems odd, since I would expect an implicit this.staticFct() - but possibly the Java compiler is smart enough)

Upvotes: 0

user1014591
user1014591

Reputation:

you should use your ClassName.StaticMethod.... to call a static method of a class

Upvotes: 1

kostja
kostja

Reputation: 61538

You should try
ClassName.dial();

The reason is that static methods belong the class itself, not to an individual instance of it. The call to instance.dial() is legal, but discouraged.

Upvotes: 2

Related Questions