Java Foundation Troubleshoot!!

Source: Internet
Author: User

Troubleshoot 1: Why can't abstract keywords coexist with private, static, final

① Private means that the subclass is not able to inherit the private method of the parent class when it modifies the method, but the method of the abstract modification must inherit and implement the quilt class, all of which conflict. static meaning, the so-called static is shared, and when it modifies the method is static method, static method is not created object can be called, when there is an inheritance relationship, abstract modified is an abstraction, can not be instantiated, the abstract method must be implemented by subclasses, and The static conflict. Final is the ultimate meaning, which cannot be inherited or rewritten, and the method of the abstract modification must be overridden by subclasses, so it is also a conflict. Therefore, abstract can only be used with public and protected.

②static:

A) static variables, variables modified by static are similar to a global variable (there is no such concept in Java). When the class is loaded by the virtual machine for the first time, the variable is allocated memory space. When the class creates an instance, a copy of the static variable is not generated. Instead, multiple instances of the class share the variable. All objects of this class can manipulate this storage space. If the final decoration is another matter. Initialization is required when the creation is complete and can be initialized directly when defined. If you need to initialize your static variable by calculation, you can declare a static block that executes only once when the class is loaded and when the class is first loaded.
Note: Static-defined variable initialization takes precedence over any other non-static variables, regardless of the order.
When it comes to inheritance, the static variables of the parent class are initialized first, then the subclasses, and so on. You can use the class name. Variable name directly and be shared by all instantiated objects of that class. Can be used by all methods in the class (Static and non-static). One of the objects in the class modifies the value of the variable, and all other corresponding values in that class object change. Defined, or initialized with a static block of code.
B) static methods, which are modified by static methods, we call them class methods. This method can be called directly through the class, without the need to create an instance of the class.
Note: You can use the class name. Method name directly. Only other static methods can be called. Only static member variables can be used. This and super cannot be referenced in any form
Purpose: Static methods often provide utilities for other classes in the application, and in Java's class libraries a large number of static methods are defined for this purpose. Arrays and collections
C) Static class, usually a normal class is not allowed to be declared static, only one inner class can. In this case, the static inner class can be used directly as a normal class, without the need to instantiate an external class.
Add: Static means "global" or "static", which is used to modify member variables and member methods, but also to form statically static blocks of code, but there is no concept of global variables in the Java language. member variables and member methods that are modified by static are independent of any object of the class. It does not depend on class-specific instances and is shared by all instances of the class. As long as this class is loaded, the Java virtual machine can find them based on the class name in the method area of the run-time data area. Therefore, a static object can be accessed before any of its objects are created, without referencing any objects.
Static member variables and member methods that are decorated with public are essentially global variables and global methods, and when you declare objects of its class, you do not generate a copy of the static variable, but all instances of the class share the same static variable.
The static variable can have a private adornment before it, indicating that the variable can be used in the static code block of the class, or in other static member methods of the class (which can also be used in non-static member methods-nonsense), but it is important that the class name cannot be referenced directly in other classes. In fact, you need to understand that private is the access permission limit, static means do not instantiate can be used, so it is easier to understand more. Static plus other access keyword effects and so on.
Static modified member variables and member methods are used to refer to statically variable and static methods, which can be accessed directly through the class name, and Access syntax is: class name. static method Name (parameter list ...) ; class name. static variable name. A static code block is represented by a statically decorated code block, which is executed when the Java virtual machine (JVM) loads the class.
Static variables: There are two types of class member variables that can be classified according to whether they are static: A variable that is statically modified, called a static variable or a class variable, and a variable that is not modified by static, called an instance variable. The difference is that there is only one copy of the static variable in memory (memory saving), the JVM allocates only one memory at a time, completes the memory allocation of the static variable during the loading of the class, can be accessed directly (conveniently) by the class name, and, of course, is accessible through the object (but this is not recommended). For instance variables, when an instance is not created, the instance variable is allocated one memory, and the instance variable can have multiple copies in memory without compromising (flexibility).

The static method can be called directly through the class name, and any instance can also be called, so the This and Super keywords cannot be used in a static method, and the instance variables and instance methods of the owning class (that is, member variables and member methods without static) are not directly accessible

③final

Features: A) A final modified variable to represent a constant, can only be assigned a value, cannot be modified. Final modified basic type variable: The value cannot be modified. Final-Modified reference-type variables (objects): Object addresses cannot be modified, and members within objects can be modified. An object reference that is defined as final can only point to a unique object and cannot point to another object, but the value inside an object can be changed. The final modified variable is a constant that must be assigned before it can be used. You can assign a value at definition, or in a constructor method. (It is possible to assign a value just before the construction method ends.) )
B) The method of final modification can not be covered by the method of quilt class;
C) A class with a final decoration cannot be inherited and has no subclasses. The final class cannot be inherited, so the member methods of the final class have no chance of being overwritten, and the default is final. However, member variables in the final class can be defined as either final or non-final forms. When designing a class, if the class does not need to have subclasses, the implementation details of the class are not allowed to change, so it is designed as the final class.
D) Final cannot be used to modify the construction method.

④static and final:static, final are used to modify member variables and member methods. For variables, this means that once a value is given it cannot be modified and is accessible through the class name. For methods that represent non-overwriting and can be accessed directly through the class name

Troubleshoot 2: The difference between abstract classes and interfaces

A) Abstract class: Class that contains the abstract modifier is an abstract class, an instance object that the abstract class cannot create. A class containing an abstract method must be defined as an abstract class, and the methods in the abstract class class do not have to be abstract. Abstract class definitions must be implemented in a specific (concrete) subclass, so there can be no abstract constructor or abstract static method. If the subclass does not implement all the abstract methods in the abstract parent class, then the subclass must also be defined as an abstract type.

B) Interface: It can be described as a special case of an abstract class, and all methods in an interface must be abstract. The method definition in the interface defaults to the public abstract type, and the member variable type in the interface defaults to public static final.

C) Difference:

    • Abstract classes can have construction methods, and interfaces cannot have constructors.
    • There can be ordinary member variables in an abstract class, there are no ordinary member variables in the interface
    • Abstract classes can contain non-abstract common methods, and all methods in an interface must be abstract and cannot have
    • A non-abstract common method.
    • An abstract method in an abstract class can have an access type of public, protected, but the abstract method in the interface can only be of the public type, and the default is the public abstract type.
    • Abstract classes can contain static methods, and interfaces cannot contain static methods
    • Both abstract classes and interfaces can contain static member variables, and the access types of static member variables in an abstract class can be arbitrary, but the variables defined in the interface can only be public static final types, and the public staticfinal type is the default.
    • A class can implement multiple interfaces, but can inherit only one abstract class.

Java Foundation Troubleshoot!!

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.