[Java in NetBeans] Lesson 05. Method/function

时间:2023-03-09 20:37:29
[Java in NetBeans] Lesson 05. Method/function

这个课程的参考视频和图片来自youtube

主要学到的知识点有:

  • Define a method:(motivation: write one time, but use it many times)

[Java in NetBeans] Lesson 05. Method/function

  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

  • static: methods can be called without declaring an object of the class first.

    3. Return type: void, int, double, char, String, boolean.

  • void: the method has no return value.

    4. Method name:

  • Good name rule: Spell out things where possible

    Lower case, second work on is capitalized, e.g.
    thisIsAFunction()

    5. Parameters:

  • Variable Scope Rule:  Variable or method parameter only exists inside the method body.
  • When pass the parameters, only pass the value. That is why when we declare a field in a class, will start with "_", to distinguish variables with the local variable here.
  • Good paramether name rule: 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

    6. Method body:

  • in the block { ... }
  • Good block format rule : Indent at 3 space

More rules about naming and format, can refer [Java] public, private, final and basic rules for naming.