DAY04----access Modifiers in the Java language
First, access modifiers overview:
An access modifier is a modification to a variable or a method or a class that implements some of the necessary permissions after the modification, mainly to explain how the class members are used.
Second, access modifiers
1. What are the access modifiers?
There are a total of 8 access modifiers, this article only first say static and final two modifiers
1.1, Final. of the modifier constants.
1.2, Static. Modifies the static
1.3, abstract.
1.4, native.
1.5, sysnchronized. of the decorated thread
1.6, transient.
1.7, volatile.
1.8, STRICTFP.
2. Concrete examples
Final and Static modifiers
package www.com; /* * Note ; * A class can define more than one class, but only one public decorated class is defined, * a class compiles and generates a. class file * */ class circle{//---------------------static member----------------- static double pi=3.14;//defines a static member that does not change as the object is created, and all objects share a variable at the same time. //can access members through the class name: double r;//non-static members are assigned a value when creating an object, and non-static members cannot be accessed by the class name. Public void area () {System.out.println (pi*r*r);} Defines a static method public static void deisplay () {System.out.println (pi); } //----------------------constant----------------- //public static final double pi=3.14;// This is a static constant pi defined, using both the static and final modifiers }public class UserCircle {public static Void main (String[] args) {//-----------------Access static Members--------------------- circle ca = new circle (); ca. Area (); ca.r = 6;//accesses the static method through the instance ca.deisplay ();/ Access------------------------------- system.out.println (CA.PI) by object name; system.out.println (CA.R);//Access---------------------------------by class name System.out.println (CIRCLE.PI);//non-static members can be accessed using the class name, //system.out.println (CIRCLE.R); Non-static members cannot be accessed through the class name. } }
Third, concluding remarks:
With this example, we can directly understand the use of final and static decorated members. Look at the notes that I wrote inside!
This article from "Lonely One Night" blog, reproduced please contact the author!
-----access Modifiers in the Java language