View Single Post
Old 07-31-2012, 09:26 AM   PM User | #1
tomjas
New to the CF scene

 
Join Date: Jul 2012
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
tomjas is an unknown quantity at this point
Why use return statement (newbie)

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?
tomjas is offline   Reply With Quote