Java static methods and variables
What is the syntax of a static method?
<class-name>.<method-name>
can you use a non static variable int a static method
No only a static variable.
example of a calling a static method
System.arraycopy();
One rule of thumb when creating objects
ask yourself "does it make sense to call this method, even if no Obj has been constructed yet?" If so, it should definitely be static.
What is a static method?
is a method which belongs to the class and not to the object. A static method can access only static data.
How can I make the public void show() method static public class StaticDemo{ public static void main(String[] args){ } class abc{ public void show(){ system.out.print("Hi"); } }
public class StaticDemo{ public static void main(String[] args){ } class abc{ public static void show(){ system.out.print("Hi"); } }
How do you declare a static variable?
static int = 5;
when might you want to create a static method?
when you want to call a method without creating the object