I'm just learning java and have come across two ways to do the same thing. My question is: what is the 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++;
}
}