Don't say you know the modifier--Drill down on Java modifiers

Source: Internet
Author: User
Tags modifiers

Tag: Modifier controls java final default modifier

    我想学习Java的朋友应该都在学习的路上遇到过这样的疑问,Java中的修饰符,对public,private,protected还算熟悉,可是对final,static等就不太清楚了,对Java中类,方法,变量的默认修饰符好像就更不了解了,我们今天就和大家一起学习一下,Java中各种各样奇妙的修饰符。
first, Scope introduction

First of all, we need to understand that there are two main functions of modifiers in Java, one is to control access, and the other is to control other aspects (such as whether the value can be changed, how it is accessed, whether it can be overwritten, etc.). And, in terms of access, generally divided into the following categories: Within the class can be accessed, within the package can be accessed, subclasses can access a range of several. We also mainly focus on these areas for analysis.

Second, the modifier of the variable

The variables in Java are mainly divided into two categories, one is a variable within a method, and one is a variable within a class (or a property or field called a Class). The variables within the method can only be final with one modifier. Represents a constant and cannot be changed. There are a lot of variables in the class, so let's take a look at the specific analysis.

1. Control of access rights:

    // 变量的修饰符,关于访问权限控制方面    publicString="public";    protectedString="protected";    privateString="private";    String="default";

We can see that there are mainly four types of modifiers, and Public,protected,private has a default. So what about their access rights?

Public: Accessible anywhere, unrestricted access.
Protected: Sub-classes within the same package or within different packages can be accessed.
Private: Can be accessed within a class.
Default: The same package can be accessed.

2. Other control aspects:

class Test{    static"static";    final"final";    }

We can see that there are two main control modifiers in other areas, static,final.

Static indicates that the variable can be accessed without having to obtain the object of the class to which it belongs. What do you mean? That is to say, for the above example, I can directly access the staticstring variable by test.staticstring way. Of course, you can also pass test test = new test (); Test.staticstring, but is not recommended for access in this way.

Final indicates that once the variable has been assigned, its value will no longer change. As shown above, if you use finalstring = "Hello" to modify its value, it will be an error, and cannot be modified.

third, non-constructor modifiers

1. Control of access rights

     //About modifier of method     Public void Printpublic() {System. out. println ("Public Method"); }protected void printprotected() {System. out. println ("protected method"); }Private void printprivate() {System. out. println ("Private Method"); }voidPrintdefault () {System. out. println ("Default Method"); }

The access rights of the non-constructors are exactly the same as the variables, which are not mentioned here.

2. Other control aspects:

    staticvoid printStatic() {        System.out.println("static method");    }    finalvoid printFinal() {        System.out.println("final method");    }    abstractvoid printAbstract();

where static and static in the variable have the same meaning. Final also indicates that in an inheritance relationship, this method cannot be overridden by a quilt class override. Abstract is a method of abstraction that requires a subclass implementation.

Iv. modifiers for constructors

The modifier of the constructor is only for access control:

class TestA {    //关于构造函数的修饰类    publicTestA(){    }    protectedTestA(int i){    }    privateTestA(int i,int j){    }    string){    }    }

Where private restricts constructors, other classes will not be able to acquire objects of this class in new ways, and can effectively control the object's creation behavior, which is very important in some design patterns.

v. Modifiers for classes

There are four main types of modifiers for classes, as follows:

// public,默认,abstract,final。publicclass Test1 {}class Test2{}abstractclass Test3{}finalclass Test4{}

The public, and the default is the access control modifier, and the variable is exactly the same, no longer repeat.

Abstract indicates that the class is an abstract class and cannot instantiate the class, that is, you cannot get an instance of Test3 by using Test3 test3 = new Test3 ().

Final indicates that the class cannot inherit from the quilt class, which is the final class and can no longer be inherited.

Vi. about Interfaces

Interface is special, the modifier of the interface is only public and default, the control permissions and variables are exactly the same, no longer repeat.

It is important to note that the variables in the interface can only be: public static final variable name. Of course, you can also not write these modifiers, the compiler will automatically help you add, because the compiler by default this is decorated.

interface, only the public and the abstract modifier, of course, you can also do not write these modifiers, the same, the compiler will automatically help you add.

PS: As to why a variable must be declared public static final please refer to my other blog: Why must member variables in the Java interface be declared as public static final?

Don't say you know the modifier--Drill down on Java modifiers

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.