Hi folks!
I am complete newbie to OOP, and generally quite new to any programming so forgive me my probably silly question. Why use return statement like this:
Code:
class Num
public int num() {
i = 10;
return i;
}
}
public class Main {
public static void main (String [] args) {
Num n = new Num();
n.num();
System.out.println(n.i);
}
}
when we can just
Code:
class Num {
public void num() {
i = 10;
}
}
public class Main {
public static void main (String [] args) {
Num n = new Num();
n.num();
System.out.println(n.i);
}
}
In all tutorials for beginners I see the former solution with return statement or the latter with even additional method which job is just return value like this:
Code:
...
public int getN() {
return i;
}
...
Why?