Implementation of the "private" keyword

Hi!

In the docs it says "The private modifier specifies that the member can only be accessed in its own class."

To me it seems that private (and protected aswelll for that matter) members are only accessable in the same OBJECT rather than the same class.

class A {
    private var _member;
    
    public function initialize(){
        
    }
    
    public function calcMember(){
        _member = doSomeFancyCalculation();
    }
    
    public static function createFromStorage() as A {
        var newA = new A();
        
        // Error: Symbol Not Found Error
        // Details: Could not find symbol '_member'
        newA._member = Storage.getValue("key_storedMember");
        
        return newA;
    }

}

Am I missing something?
This is weird, right?

It makes something like a static factory method very cumbersome to implement.

Let me know if I've missed something here. If not, I think the docs should be updated and this restriction (compared to e.g. java) should be mentioned.

Top Replies

All Replies

  • With your code and with the "newA" you return, the caller shouldn't be able to access _member.

  • it's not about the caller.

    The access in line 17 fails. So you can't access A's private member _member from within (a static function of) A.

  • Hmm this is pretty interesting. The docs seem wrong here, and the behavior is definitely different from that of C++, C# and Java.

  • I'd say the docs are right and the implementation is wrong. I'd suggest to file a bug report.

  • Everything is OK.

    private means privates according to class only class's functions can access  privates.

    class A

    {//<- start private area

    //private area where you can access private members and functions

    }//end private area

    change to:

    public var _member;

    an will be ok than if you want to access _member outside class's functions

  • you _do_ realize that createFromStorage() is within the class, right?

  • no, unfortunately is outside the class  because you add word static, so it means this function can be called without any object of this class so it's out of privete area

  • no, unfortunately is outside the class  because you add word static, so it means this function can be called without any object of this class so it's out of privete area

    Wow that's interesting, maybe you could explain why the following C++ code compiles and runs without error?

    #include <iostream>
    
    class A {
        private:
            int _member;
    
        public:
            static A *foo() {
                A *newA = new A();
                newA->_member = 42;
                return newA;
            }
            int getMember() {
                return _member;
            }
    };
    
    int main() {
        int val = (A::foo())->getMember();
        std::cout << "val = " << val << std::endl;
        return 0;
    }

    I'm fairly sure similar code in C# and Java would work, as well.

    Just because a method is static doesn't mean it isn't part of the class. And as OP said:

    In the docs it says "The private modifier specifies that the member can only be accessed in its own class."

    So even if we don't care about other languages or the common understanding of "private", the documentation should reflect how it's actually supposed to work (or vice versa)

  • Wow that's interesting, maybe you could explain why the following C++ code compiles and runs without error?

    Because C++ was always a joke of a language?  It was never really OOPS since day 1!

  • Maybe you are right and maybe in c++/java it runs well but practically there should be no difference between static class function and global function - only one accessing: foo()/class.foo(). And for me as I can't access private members of class from foo() I couldn't access it form static class.foo() too.