Search code examples
javaprogramming-languagesprivatepublic

Learning java, why are my privates private?


Im learning java, coming from python there are quite a few things I don't fully understand that exist in java and the first of these has to be public and private declarations. I mean from a language that has no real visible public private declarations to a language where everything MUST be private, I understand the ground principle about what they do. Im asking 'why' they do it. Why should anyone care who touches there private parts? It shouldn't really be a problem if your a good programmer you know which bits you 'should' and 'should not' poke in code. So why the secrecy? Why hide and obscure and make things private from the world? Why care to start with.


Solution

  • (Opinionated answer follows.)

    This is because Java is a bondage and discipline language, designed to cater for mediocre programmers. Python, otoh, is a language aimed at consenting adults who agree to expose their objects' private parts to each other, trusting other programmers not to abuse the undocumented/unguaranteed functionality offered to them. If they do and anything breaks, the person who broke stuff gets to keep the pieces.

    Python does have some access control, such as __all__ to hide/show parts of a module, it's just that all of it is optional and can be by-passed at the risk of the person doing so.

    (To repeat what I said in the comment: I'm not saying that all Java programmers are mediocre. I'm just saying that the language is designed so that it can safely be used by such programmers.)