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(); } }

17th Oct 2017, 1:21 PM
oyl
3 odpowiedzi
+ 2
Yes you have given a correct explanation in your code for static methods.
2nd Nov 2017, 2:03 AM
Rrestoring faith
Rrestoring faith - avatar
+ 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();
17th Oct 2017, 3:39 PM
Chriptus13
Chriptus13 - avatar
0
@André Martins so i have explain it correctly right?
18th Oct 2017, 12:57 AM
oyl