java - What is the difference between these two implementations? -
i'm learning java , have come across 2 ways same thing. question is: difference between them? thanks.
option a:
class foo { public static int var; foo (){ var = 0; } public static void main(string[] args) { foo object = new foo(); object.method(); system.out.println(object.var); //prints 1 } public void method (){ var++; } }
option b:
class foo { public static int var; foo (){ var = 0; } public static void main(string[] args) { foo object = new foo(); method(object); system.out.println(object.var); //prints 1 } public static void method (foo object){ object.var++; } }
to reinforce @eckes mentioned above -
there's more common in samples different - both obscure , promote bad practice understanding class members
note: can refer static fields object reference
mybike.numberofbicycles
but discouraged because not make clear class variables.
clean way increment static variable in case: foo.var++
, in real life example it's wrong have mutable static variables. if your're beginner.
Comments
Post a Comment