Static should be applied to:
Examples:
Static imports allow us to access static variables and static methods without typing the class name
To access a static variable or static method, we can use:
[Classname].[static variable name]
or
[Classname].[static method name]
We can import using:
import static java.lang.Math.*
this means, import all the static variables and static methods inside the Math class and allows us to say things like
double t= sqrt(PI); // instead of
double t= Math.sqrt(PI);
e.g.
import static java.lang.System.*; import static java.lang.Math.*; public class Test { public static void main(String[] args) { double t=sqrt(PI); // instead of Math.sqrt(PI) out.println(t); // instead of System.out.println(t) } }
Output:
1.7724538509055159
Good video on how static and non static variables and methods interact with each other.
Static can call non static, no problems. But static cannot call non static without the create of an object. Static lives in the world of none objects. However, non static CAN call static directly.