Some of the keywords in Java are sometimes confusing to beginners, and you can sort them out here and comb through the key words you're currently mastering.
Permission modifiers
There are four, permissions from large to small are public>protected>defaul (no modifiers) >private, you can modify member variables and member methods. Private can only be called in this class, used to encapsulate member variables, the others are relatively easy to grasp.
this keyword
Represents a reference to the current class, that is, which object calls the method, and the inside of the method represents that object, such as code:
1 classStudent {2 PrivateString name;3 Private intAge ;4 5 PublicString GetName () {6 returnName//This is actually implied here.7 }8 9 Public voidsetName (String name) {Ten This. name = name;//because of the proximity principle, if the name does not precede this, then the name here refers to the same variable, that is, the local variable here One } A - Public intGetage () { - returnAge ; the } - - Public voidSetage (intAge ) { - This. Age =Age ; + } - } + A classStudentTest2 { at Public Static voidMain (string[] args) { - //Create an Object -Student S1 =NewStudent (); -S1.setname ("Brigitte"); -S1.setage (27); -System.out.println (S1.getname () + "---" +s1.getage ()); in - //Create a second object toStudent s2 =NewStudent (); +S2.setname ("Liu Yifei"); -S2.setage (30); theSystem.out.println (S2.getname () + "---" +s2.getage ()); * } $}
Static keyword
You can modify member variables and member methods into static variables or static methods. Static characteristics: 1. As the load of the class loads, it takes precedence over the existence of the object; 2. Shared by all objects of the class, such as the Chinese attribute, which can be understood by the characteristics of the water dispenser and the cup; 3. Can be called by the class name. It is important to note that there is no this object in the static method because the static method takes precedence over the existence of the object, that is, the object has already been loaded with the class. In other words, static methods can only invoke static, non-static methods do not have this rule.
The key words in the Java Self-study notebook--java