View Single Post
Old 07-31-2012, 03:16 PM   PM User | #3
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
Quote:
Originally Posted by Fou-Lu View Post
I'm not sure why you think this will work:
PHP Code:
class Num {
    public 
void num() {
        
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?
tomjas is offline   Reply With Quote