代码改变世界

[Java] public, private, final and basic rules for naming.

2018-12-12 04:00  Johnson_强生仔仔  阅读(283)  评论(0编辑  收藏  举报

  1. Access: publicprivate, 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 : [#############]