[Go to]java constructor method's access modifier

Source: Internet
Author: User
Tags modifiers

http://my.oschina.net/u/1464678/blog/210359

1. Classes (Class)

Visibility modifier: public- is visible in all classes and can be imported with import in other packages.

Default- is that there is no modifier, which is visible in the class in the same package and cannot be imported with import in other packages.

modifier:final-end-state class, indicating that the class cannot be inherited

Abstract - Abstract class, cannot create new object

2. Variables (variable)

There are two types of variables in Java, method variables and field variables

Method variables can only be used with one modifier:final-represents constants, cannot be modified

The following is the modifier for the field variable

Visibility modifier: Public-visible in any class

protected-visible in subclasses or in the same package

Private-only visible in this class, not visible in subclasses

Default- visible in the same package, subclasses are not in a package, and are not visible in subclasses

modifier: staticvariable, shared by all instances of the class

final-constant, cannot be modified after definition

*transient-tells the compiler that this variable does not need to persist when the class object is serialized

*volatile-Indicates that there may be multiple threads modifying this variable, requiring compiler optimizations to ensure that modifications to this variable are handled correctly

These two are not very common, but also Java modifiers, I am not very clear

3. Methods (method)

Visibility modifier: Public-visible in any class

protected-visible in subclasses or in the same package

Private-only visible in this class, not visible in subclasses

Default- visible in the same package, subclasses are not in a package, and are not visible in subclasses

Modifier: Static method, which does not need to create an instance of the class to access the static method

final-constant method, all subclasses cannot overwrite the method, but can overload

Abstract-a method that is not implemented in an abstract class

native-local method, see Java Native Interface (JNI)

synchronized- in multi-threading, all other methods cannot call the method when the Synchronized method is called

4. Interface (interface)

Visibility modifier: Public-visible in all classes

Default -visible in the same package

The variables in the interface must always be defined as "public static final interface name", but can not contain these modifiers, which is the compiler's default

Because there is no direct instance of an interface, it is impossible to modify (final) or to access (static) through an instance.

Methods in an interface can only use the public and abstract modifiers

==================================================================================================

Public: can be accessed by any class. Final: Classes that use some modifiers cannot be inherited. Abstract: If you want to use the abstract class, you need to create a new class that inherits from it to implement the abstract method. Variables: There are no global variables in 1,java, only method variables, instance variables (non-static variables in the Class), class variables (static variables in a Class), 2, variables in a method cannot have access modifiers, so the access modifier is only for variables defined in the class, 3, if no initial value is assigned when declaring an instance variable, will be initialized to null (reference type) or 0, False (primitive type), 4, you can first make a more complex instance variable by an instance variable, the first instance variable is a block of statements with {}, run when the constructor of the class is invoked, run after the parent class constructor, before the constructor ; 5, the class variable (static variable) can also be initialized by the variable initializer, the class variable's starter is a block of statements that are included with static{}, and can only be initialized once.   Access modifier: public: Can be accessed by any class. Protected: Can be accessed by all classes in the same package, or can be accessed by the quilt class, even if the subclass is not in the same package. Private: can only be accessed by methods in the current class. Default: No access modifier, can be accessed by all classes in the same package, if the subclass is not in the same package, inaccessible.   Modifiers: Static variables (also known as class variables, instance variables, and so on) can be shared by all instances of the class and can be accessed without creating an instance of the class. Final: constant whose value can only be assigned once and cannot be changed; be careful not to use const, although it has the same meaning as the Const keyword in C and C + +. Transient: Tells the compiler that this variable does not need to be persisted when the object of the class is serialized. The main reason for this is that this variable can be obtained by other variables, which are mainly used in terms of performance. Volatile: Indicates that there may be multiple threads modifying this variable, requiring the compiler to optimize to ensure that modifications to this variable are handled correctly.   Method: 1, the constructor method of a class cannot have modifiers, return types, and throws clause 2, when the constructor method of a class is called, it first calls the constructor method of the parent class, and then runs the instance variable and the initialization of the static variable before the class constructor itself is run. 3, if the constructor method does not display a constructor that calls a parent class, the compiler automatically adds a default super (), and if the parent does not have a default parameterless constructor, the compiler will give an error. Super () must be the first clause of a constructor method. 4, note the techniques used by the private class constructor method.  static: Static methods, also known as class methods, and instance methods, provide services that do not depend on class instances, that is, you do not need to create class instances to access static methods. Final: Prevents any subclasses from overloading the method;To use const, although it has the same meaning as the Const keyword in C, C + +. Abstract: A method that is declared but not implemented in a class, cannot declare a static method, a final method, and a constructor method as abstract. Native: The method decorated with this modifier is not implemented in the class, and in most cases the implementation of such a method is written in C or C + +. See Sun's Java Native Interface (JNI), which provides the runtime to load an implementation of a native method and associate it with a Java class. Synchronized: Multithreading support, when a thread calls the method, it adds a lock to the object, so that other threads cannot call the method again until the previous thread releases it. Interface: 1, an interface cannot define any implementation of its declared methods. 2, the variables in the interface always need to be defined as "public static final interface name" or can not contain these modifiers, but the compiler defaults to this, the display contains modifiers mainly for the program logic clear.

[Go to]java constructor method's access modifier

Related Article

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.