The following notes are based on video series: 手把手教你用Java基础教程 - 北京尚学堂 - 马士兵
Keyword this
- In a method of a class ,
this
refers to the object that uses the method. - Sometime it can be used to deal with the case that member varibales and method parameters have same names. (ref. uses the closest def.)
this
can be view as a variable, whose value is the reference of the current object.
Keyword static
- In a class, a
static
member variable is defined to be a shared variable for the class. When the class is first used, thestatic
variable is initialized. For all the instance of the class, there is only one copy of this varible. - When a method is defined as
static
, this method, as it is called, will not parsed the ref. of the instance to the method, and hence will not able to use non-static
variables. - We can use either instance ref. or class name (without a instance) to access
static
members.
1 | // ex: use static var as counter |