Static function

+2 Arnab Banerjee · February 3, 2015
What it is meant by a static function ??

Post a Reply


Oldest  Newest  Rating
+1 Nicholas Eason · February 3, 2015
Static means that you don't need to instantiate, or create, and object of the class to access the method, or function.

If you have a public static method, a class called, say Pie, can access a class called, say Fruit, and use it's method, say apples.

So you simply call 

within a code block and it's completely valid. However, if the method is not static, you must create an object of the class, and the process takes longer, but is usually more proper way of doing things.
Fruit apple = new Fruit();

The reason it's cleaner is because you get access to things you didn't previously have access to. Such as, non-static variables and methods. Also, when you're editing a variable of apple, it's not going to impose a long-term effect to that code. i.e
Fruit apple = new Fruit();
apple.number = 1;
Fruit orange = new Fruit();
orange.number = 2;

There you aren't editing the "same" variable. Of course the variable seems the same, but in reality, you have 2 different instances of this variable. Whereas:
Fruit.number = 1;
Fruit.number = 2;

Above, you had the same intention as with the non-static approach, except you've gone and edited the same variable.
I hope this made sense, it's a fairly rough explanation.
0 Crazy Ninja · February 11, 2015
Simply, if you have a Static method or a member variable, That method or variable is exist one per JVM. It lives standalone to the class existence.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator