Many friends don't know the usage of public, static, private and protected, especially static and protected. Although they know how to use them, they don't know what their practical significance is. Xiaobian, let's explain it for you!
Before the explanation, draw a summary table for everyone (Y is visible and N is invisible)
Access package location / class modifier | public | private | protected | |
This category | Y | Y | Y | |
Sibling other classes or subclasses | Y | N | Y | |
Classes or subclasses of other packages | Y | N | N |
Usage of public
The keyword public must be familiar to everyone. Novices know that they need to use it when defining classes.
public class Test { public static void main(String[] args) { } }
The objects defined by public are all public. If you don't believe it, let's create another Test2 class (under the same package, or Import), and enter the following code in the Test class (the location where the code is written is not in the main function):
public class Test { public static int PI = 3.1415926535; //Omit some code }
Enter the following code in Test2.java.
public class Test2 { /** *Main method */ public static void main(String[] args) { //Use this variable System.out.println("PI is:"+Test.PI); } }
The result is that it outputs "Pi: 3.1415926535". Obviously, it is successful. This is public.
Usage of static*
Although static has been used, and it has also been used when public defines variables, many small partners still don't know what it means. It means "static". Let me explain it for you.
Sometimes when dealing with problems, you will encounter the need to share a data in the same memory area between two classes. For example, gender is needed in Xiaoming class, and it is also needed in Xiaogang class. If you want to create sex variables in two classes at the same time, it is troublesome. For this problem, we can turn sex into a static variable.
Note: static members belong to classes. Different from individual objects, they can be called in any class with "my class. This variable".
Example: create a StaticTest class, define a variable and a function (using static) in it, and call it in the main method.
public class StaticTest { //A variable created with the static keyword is created static String name = "JavaIdot" public static void main(String[] args) { /* *This prompt in this Code: *this Keyword is a key point, which refers to this class. *But in other classes, you still need to write the name of the class (StaticTest.name) *It has many uses, for example *If the name of the global variable of this class appears in the construction method (API learning), such as name, *You write name = name;, The program will report an error, *So, if you write this.name = name;, Then it will work normally. */ this.function(); System.out.println("my csdn Name is"+this.name+". "); } public static void function() { System.out.println("This is static The best way!"); } }
Operation results:
This is static The best way! my csdn Name is JavaIdot.
Usage of private
Private means "private". As the name suggests, it can only be visible and used in this class. For example, you create a test class and enter the following code:
public class test { private int PI = 3.14; public static void main(String[] args) { System.out.println("PI (two decimal places):" + PI); } }
The result is "pi (two decimal places): 3.14". Yes, it can be used in this class, but in other classes? You might as well create another class, test2.java.
public class test2 { public static void main(String[] args) { System.out.println(test2.PI); } }
The result is a system error. Sure enough, it can't.
However, when we write high-level programs in java, there will be a function similar to "getXXX()" in the class, which is public. Its function is to get XXX in other classes. Let's clear the codes of test and test2 and enter these codes:
/** *test Code of */ public class test { private int num = 123456; /** *This is what we call getXXX() */ public int getNum() { return num; } }
Enter the main method in test2, enter "System.out.println(test.getNum())" in the method, and the result is "123456". This is private and getXXX.
Usage of protected
Protected doesn't seem to be commonly used. Xiaobian hasn't used it at all. According to the description, it is only for this class and subclasses, which means "protected". But I think JDK developers probably use it often (because some swing components may use protected).
The above is the usage of public private protected static. If you like it, remember to pay attention and like it!