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

本文详细介绍了Java中public, private, protected的使用场景,static与final的特性,以及良好的类名、方法名和变量名的命名规则,帮助开发者更好地理解和应用这些核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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 : [#############]

转载于:https://www.cnblogs.com/Johnsonxiong/p/10090132.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值