-----access Modifiers in the Java language

Source: Internet
Author: User
Tags modifier modifiers

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

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.