![[Java in NetBeans] Lesson 05. Method/function [Java in NetBeans] Lesson 05. Method/function](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
这个课程的参考视频和图片来自youtube。
主要学到的知识点有:
- Define a method:(motivation: write one time, but use it many times)
- 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.