I was helping a friend on his Java homework today, and I guess I didn’t realize there being a difference between plain Java and Java in Android. Quick write up of the program:
public class myClass{
public static void Main (String[] args){
doThis();
}
public void doThis(){
System.out.println("Did this");
}
}
But when running that, I got a complaint that I needed to make the doThis() method to be static. Why is that? When I develop some basic things in Android, I never have to use the static keyword.
Note: This could stem from the fact that I’m intimidated by what static actually means.
Because
Main()is static.If
Main()was an instance method, and you had calledMain()on an instance ofmyClass(e.g.,new myClass), thendoThis()could also be an instance method. Or, if your staticMain()created an instance ofmyClass, it could calldoThis()on that instance.That is because your entry points in Android tend to be instance methods on components (e.g.,
onCreate()of anActivity.