The key words in the Java Self-study notebook--java

Source: Internet
Author: User
Tags modifiers

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.