java--keywords and Cosmetic properties

Source: Internet
Author: User
Tags modifier modifiers

1. Modifier properties in Java (Public,private,protect,default)

Public: The most restrictive modifier for access in Java. Classes, properties, and methods that are modified by them can be accessed not only across classes, but also across packages (package)

private: The modifier with the narrowest permission limit. Classes, properties, and methods that are modified by them can only be accessed by objects of that class, cannot be accessed by subclasses, and cannot be accessed across packages

protect: between public and private. Classes, properties, and methods that are modified by them can only be accessed by methods and subclasses of the class itself, even if the subclasses are accessible in different packages

default access modifier, accessible only in one package

2. keyword static in Java

(1) static modifiers or global variables (it opens up space at compile time, and there is only one that can be used to make global variables that can be accessed through the class name), and the other variables are called instance variables.

(2) The static Decoration method, which represents the entire class of methods, does not need to create an object to invoke, can be called through the class name, such as collections and arrays ( array of Operations ) is the entire method of static decoration. Methods that are not static modifiers must be new to an object before they can be called. In addition, only the static method can be called in the static method. public static void Main (string[] args), main must be modified with static.

(3) statically static blocks of code that are independent of member variables and member functions in a class. The variable used to initialize the global, and will only be executed once.

(4) Static inner class: Only static member variables and static methods of external classes can be accessed.

3. Keyword final in Java (Ultimate modifier)

(1) Modify the basic data type, you can think that the data is a constant, the value can not be changed

(2) The object that modifies other classes, the binding relationship between the data and the object it points to is immutable, and the properties inside the object can be changed.

(3) The final modification of the method is not covered by the quilt class, but the final modified method can be overridden by the quilt class (not overwritten, can be overloaded), can be inherited

(4) The final modified class cannot be inherited, and the method in it must be the final method (default), no more subclasses

(5) used as static final, because the final specified property is either not changed or the binding relationship cannot be changed, so plus final can save memory

(6) Final cannot modify the constructor method because the subclass uses the parent class's constructor by default

Java is a development from C + +, in C + +, a class can inherit multiple parent classes at the same time, but in Java, classes can only inherit single-

4. The keyword in Java finally

Finally-decorated blocks of code are bound to execute, and a finally is performed before the return of the try and catch modules.

Try{}catch () {}catch () {}catch () {}finally{} multiple catch to capture, finally writes the final statement to execute, regardless of whether the exception occurs, executes the statement in finally

5. Polymorphism in Java

Compile-time polymorphism, compile time to determine the specific method of the body of the call, such as the overload of the function (different parameters) run-time polymorphism, the runtime to determine the specific method of the call, the inheritance of the method overrides (subclass method overrides the parent class method)
Java.lang.Object class: The parent class of all classes, all classes in Java are inherited from the object class public Boolean equals (Object obj): Two objects are judged to return true, only two objects are pointed to the same object, and Truepublic is returned. String toString (): Each class can overwrite the output of the class implementation string according to its own requirementsproperties are hidden: The occurrence of a property with the same name as the parent class in the subclass, calling the subclass function to handle the properties of the subclass, calling the parent class function to handle the parent class's propertiesoverride of Method: A behavior in a subclass that has the same method header method in the parent class is defined in the inheritance, the constructor of the parent class is called first, and then the constructor of the child class is calledSuper keyword: You can call directly the hidden properties and methods in the parent class super (argument): Call the constructor of the parent class super. Parent Class Property: Call the Parent class's properties (non-hidden properties can be used directly, do not need super) this and super cannot be used within the static Decorated method objectinstanceof Class Name: Determines whether an object is an instance of a class

java--keywords and Cosmetic properties

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.