0
static question(beginner)
can someone verify if i have given the right explanation for the use of static?(1 with static and 1 without static) //code1 class Phone{ void sound() {System.out.println("apple!");} } class Myclass{ public static void main(String[]args){ Phone apple=new Phone(); apple.sound(); } } ----------------------- //code 2 class Phone{ static void sound() {System.out.println("apple!");} } class Myclass{ public static void main(String[]args){ Phone.sound(); } }
3 Answers
+ 2
Yes you have given a correct explanation in your code for static methods.
+ 1
Static members are class level and can be accessed directly without any instance.
While with non-static members you need an instance (Object) of that class to access them!
So static methods are called like:
ClassName.method();
And non-static methods:
obj.method();
0
@André Martins so i have explain it correctly right?