Static and final keyword explanations

Source: Internet
Author: User

    1. Interface (interface): The interface is equivalent to class. All the methods in the interface are abstract methods. When you declare a method in an interface, you can use the abstract keyword or you can not use it. Typically, the abstract keyword is omitted.

    2. Can treat interfaces as special abstract classes (abstract classes can have concrete methods or abstract methods, and interfaces can only have abstract methods and no concrete methods). The

    3. Class can Implement the interface. Implementations are represented by implements, which represents a class that implements an interface.

    4. A class implements an interface, so the class must implement all the abstract methods declared in the interface. If the class is an abstract class, then you do not need to implement the methods in the interface.

    5. java is single-inheritance, which means that a class can have only one parent class, one class may implement multiple interfaces, and multiple interfaces are separated by commas.

    6. Polymorphic: A so-called polymorphic, that is, a reference to a parent type can point to an object of a subtype, or a reference to an interface type can point to an instance of the class that implements the interface. The forced type conversion between the interface and the class that implements the interface is exactly the same as the coercion type conversion between the parent class and the child class.

    7. Static keyword: can be used for cosmetic properties, can be used for cosmetic methods, and can also be used to decorate classes.

    8. Static Decorated property: No matter how many objects a class generates, all of them use a unique static member variable, and one object modifies that static member variable, and the value of that static member variable for the other object changes as well. If a member variable is static, then we can use it by (Java recommends this way).

Package com.pb.polytest;public class statictest {public static void main ( String[] args)  {person p = new person (); Person p2 = new person ();p .age = 20 ; System.out.println (P2.age);}} class person{int age;//does not use the static keyword to decorate the property} results show: 0 The variable p and the variable P2 each point to the object has its own property value, change the value of the object that P points to, There is no effect on the property value of the object pointed to by P2. Package com.pb.polytest;public class statictest {public static void main ( String[] args)  {person p = new person (); Person p2 = new person (); Person p3 = new person ();p .age = 20 ; System.out.println (P2.age); System.out.println (P3.age);}} class person{static int age;//uses the static keyword to decorate the property} results show:20  proof: No matter how many objects a class generates, All of these objects work together with a single static member variable, and one object modifies that static member variable, and the value of that static member variable for the other object changes as well. package com.pb.polytest;public class statictest {public static void  Main (String[] args)  {Person.age = 20; Person p = new person (); System.out.println (P.age);}} Class person{static int age;} Results showed: 20

9. Static Modification Method: The method of static modification is called a statically method. For static methods, you can use the class name. Method name to access it.

static methods can only inherit and cannot be overridden.

Final keyword: final you can modify properties, methods, classes.

Final Modified class: When a class is modified by final, it means that the class is an end-state class and cannot be inherited.

Final modification method: When a method is final decorated, it means that the method is an end-state method, that is, it cannot be overridden (override).

Final Modified properties: Indicates that the property cannot be overwritten when a property is modified by final.

When final modifies a native data type, the value representing the native data type cannot be changed (for example, it cannot be changed from 10 to 20), or if the final decoration of a reference type means that the reference type can no longer point to another object. However, the contents of the object to which the reference refers are variable in amount.

For a final type of member variable, there are generally two ways to assign an initial value: a) to assign an initial value when declaring a member variable of the final type. b) The initial value is not assigned when declaring a member variable of the final type, but it is assigned an initial value in all the constructor methods of the class.

. Static BLOCK: Static code block. The function of static code blocks is also to do some initialization work. Execute the static code block first, and then execute the construction method. Static blocks of code are executed when the class is loaded, and the constructor is executed when the object is generated, and to invoke a class to generate the object, you first need to load the class onto the Java Virtual Machine (JVM) and then the JVM loads the class to generate the object.

18. The static block of code for a class executes only once, when the class is loaded, because each class is only loaded once, so the static code block is executed only once, and the constructor does not, and the constructor of the class is called every time an object is generated, so the new one invokes the constructor once.

19. If the inheritance system has both a construction method and a static block of code, first execute the static code block of the topmost class, execute to the lowest static code block, and then execute the construction method of the topmost class, and execute to the lowest-level construction method. Note "Static code blocks are executed only once."

20. Non-static member variables cannot be accessed in a static method; static members can be accessed in static methods bianliang.keyi static member variables are accessed in non-static methods.

Summary: Static only access to static, non-static can access everything.

22. You cannot use the This keyword in a static method.










This article is from the "Java" blog, so be sure to keep this source http://5737386.blog.51cto.com/5727386/1661125

Static and final keyword explanations

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.