Quote:
Originally Posted by Fou-Lu
I'm not sure why you think this will work:
PHP 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);
}
}
Even if that main were in the class Num, it won't have access to 'i' since it's not declared as a member property (or even a local one so that code for Num would just fail to compile).
Public properties should be avoided as you cannot run any verification or validation if it is left public. It should be private or protected, and accesor and mutator functions written for them. If the main were in a class with a private property, it can still access that property, but it cannot access it if its not a part of the same class.
|
My mistake - forgot about declaration of "i". Now it works:
Code:
class Num {
int i;
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);
}
}
You are saying that the only reason of using a return statement is validation? So changing num() method to private/protected and writing still public method which invokes it and returns its value will enable validation?