Java access modifier

Source: Internet
Author: User
Tags modifiers variable scope

This topic examines modifiers, function definitions, etc., so from the Internet to gather the relevant information, summarized as follows:

Available modifiers for classes, methods, member variables, and local variables

Modifier

Class

Member adapting

Construction method

Member variables

Local variables

Abstract (abstracted)

Static (statically)

Public (Common)

Protected (Protected)

Private (privately)

Synchronized (synchronous)

Native (local)

Transient (temporary)

Volatie (volatile)

Final (do not change)

class modifier

Public

can be accessed from other classes

Abstract

This class cannot be instantiated

Final

Subclass can no longer be declared

constructor Modifier

Public

can be accessed from all classes

Protected

can only be accessed from its own class and its subclasses

Private

can only be accessed in this class

Domain / member Variable modifiers

Public

can be accessed from all classes

Protected

can only be accessed from this class and its subclasses

Private

It can only be accessed from this class

Static

Only one domain value exists for all instances of the class

Transient

is not part of an object's persistent state

Volatile

Can be modified by an asynchronous thread

Final

It must be given an initial value and cannot be modified

Local Variables modifier

Final

It must be given an initial value and cannot be modified

Method Modifiers

Public

It can be accessed from all classes

Protected

It can only be accessed from this class and its subclasses

Private

It can only be accessed from this class

Abstract

No method body, belongs to an abstract class

Final

Subclasses cannot overwrite it

Static

is bound to an instance of the class itself rather than the class

Native

This method is implemented by other programming languages

Asnchronized

Before a thread calls it, it must first add the

Class modifiers are integrated

A Class

Modifiers for the class:

public: Can be used in any other class, default to any class under the uniform package.

Abstract: Abstract Classes , cannot be instantiated, can contain abstract methods, abstract methods are not implemented, no specific functions, can only derive subclasses.

Final: Cannot be inherited.

Two Variable

Variable modifiers:

The declaration of a member variable of a class must be in the class body, not in a method, and a local variable is declared in the method.

1. Accessible modifiers:

2. Static: class variable : A variable owned by a class, not a variable for each instance of the class. A class variable is one that allocates memory for a class variable only the first time the class is called, regardless of how many objects the class creates, and all objects share the class variables of that class, so the class variable can be accessed through the class itself or an object.

3. Final: constant .

4. volatile: Declares a variable that is controlled and modified by several threads that may be concurrently running.

instance variable : corresponds to a class variable, that is, each object has its own independent instance variable.

three. Methods:(and variable objects are divided into example methods and class methods, and with no static modification of the difference)

class Methods : Methods described using the static keyword

1. The first call to a class with a class method is that the system only creates a version of the class that is shared by the class and all instances of the class.

2. Class methods can only manipulate class variables and cannot access instance variables. A class method can be called in a class without having to create an instance to invoke, or it can be called through an object.

Instance methods: Instance methods can operate on instance variables of the current object, and can access class variables.

Methods can be overloaded and require the same method name, but the parameters must be different. (different parameters can make different types, different order, the number of different)

The return type of the method: if there is no return type, it is declared void.

Variable scope in the method:

1. Member variables: the entire class.

2. Local variables: Defines the end of the method block.

3. Method parameters: The entire method or construction method.

4. Exception handling Parameters: Parameters are passed to the exception handling method.

constructor Method : A method with the same name as the class. Initializes the newly created object after creating a memory space for the new object. cannot be explicitly called with an object.

static initializer : Format:static{< assignment statement group;}

The difference between a static initializer and a construction method:

Static initializers

Construction method

Initializes the static domain of the class.

Class for the newly created object.

After the class enters memory, the system call executes

Automatic execution after new execution

Special statement (only once)

belong to a special method

Modifier for method:

abstract methods: with the abstract modification, only the declaration part, the method body is empty, specifically in the subclass to complete.

class Method: static method, modified with Static,

1. When called, use the class name as a prefix, not an instance object of the class

2. Cannot be owned by a single object, belonging to the entire class share.

3. Member variables cannot be processed.

The final method : A method that cannot be redefined by the quilt class with final decoration.

Local Methods : The method of native modification is used to denote special methods written in other languages, including C,c++,fortran, assembly language, etc.

Four. Access control for class members :

That is, the method of the class and the access control of the member variable, a class that is not visible as a whole object, does not mean that all his fields and methods are not visible to other parts of the program, and that they need to be judged by their access modifiers.

Permissions are as follows:

Access modifiers

The same class

Same package

Different packages, sub-categories

Different packages, non-subclasses

Private

Protected

Public

Default

Java access modifier

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.