1. Access: public, private, protected
- public: Any other class can access a public field or method. (Further, other classes can modify public fields unless the field is declared as final.)
- private: Only current class can access.
- protected: Accessible within all classes in the same package and within subclasses in other packages.
2. Declaration: static, final
- static: methods can be called without declaring an object of the class first.
- final: Means that constant, can not be modified.
3. Naming rules
- Good class name rules: Words are capitalized , e.g.
ThisIsAClass - Good mehod name rules: Spell out things where possible
Lower case, second work on is capitalized, e.g.
thisIsAFunction() - Good variables name rules: Spell out things where possible
Lower case, second work on is capitalized, e.g.
numberRuns - If it is final : All caps with underscores, e.g.
THIS_IS_A_CONSTANT - If it is class members: Begin with underscores unless they are public, e.g.
private String _thisIsAnInternalVariable
System.out.printf("%2d : [",numberRuns);
Enter value for A : 4
Enter value for B : 13
4 : [####]
13 : [#############]
本文详细介绍了Java中public, private, protected的使用场景,static与final的特性,以及良好的类名、方法名和变量名的命名规则,帮助开发者更好地理解和应用这些核心概念。
80

被折叠的 条评论
为什么被折叠?



