Java Interview Fourth day

Source: Internet
Author: User

Modifier static: Relates an object to a class-related, which can modify properties, methods, code blocks, and inner classes

Static modifier property (class variable):

Then this property can be accessed with "class name. Attribute name", which makes this property a class variable of this class, shared by this class of objects.

Class loading process, the class itself is also stored in the file (bytecode file holds the information of the class), Java will pass the I/O stream to the class file read into the JVM (Java Virtual machine), this process is called class loading. The JVM uses the Classpath (CLASSPATH) to find the bytecode file. Class loading is required, and when the object is built, it is first loaded and then constructed

Class variables are automatically initialized at load time, and the initialization rules and instance variables are the same.

Attention:

instance variables in a class are initialized when the object is created.

The static decorated property is created and initialized when the class is loaded, and the class is loaded only once, that is, the class variable is only created once.

Static Modification method (statically method):

Makes this method public for the entire class and can be accessed using the class name. Method Name.

Static modified methods that do not directly access non-static members of this class, but non-static methods of this class can access the statically members of this class.

The This keyword cannot be present in a static method.

A static method in a parent class that cannot be overridden as a non-static method, in which a static method in a parent class can be overridden by a static method in a child class, but not polymorphic, under the premise of conforming to the override rule. (a static method that calls a compile-time type when using an object to invoke a static method)

The main method in Java must be written as a reason for static: The object cannot be created while the class is loading, and the static method can not be called through the object, so you can run the program through the main method portal when the class loads.

Static modifier initial code block:

This initial block of code is then called the static initial code block, which is only executed once when the class is loaded.

You can initialize a class with a static initial block of code.

Dynamic initial code block, written in the class body of "{}", this code block is run when the object is generated, this block of code is called dynamic Initial code block. static{}

Single Case design mode:

A class allows only one object, guaranteeing that all referenced objects are the same object.

Because only one object is allowed, the new object is not allowed to be created directly outside, so the constructor should be set to private.

Define a public static method within a class that allows the user to make a call to obtain an instance of the method.

Cases:

Public CALSS singleton{

private static Singleton s;

Private Singleton () {}

public static synchroinized Singleton newinstance () {

if (s = = null)

s = new Singleton ();

return s;

}

}

Modifier final: Not allowed to change, can modify variables, methods, classes

Final Modified variables:

The final modified variable becomes constant once the assignment cannot be changed

Constants can be directly assigned at initialization time, or can be assigned in the constructor method, only two of the two methods can be selected, cannot be assigned to a constant value

Constants do not have default initial values

Lock the stack so that the data in the stack cannot be changed

Static constants can only be assigned directly at initialization time

Final Modification Method:

The final modified method cannot be overridden by its subclasses, and the stability of the method cannot be overridden

Final Decorated class:

A class that is final modified cannot be inherited

The methods in the final class are also final.

Attention:

Final cannot be used to modify the construction method

Access Rights control:

Private

Within this class you can access

Cannot inherit to child class

Default

This class can be accessed internally, and other classes of the same package can also be accessed.

Same package can inherit

Protected

This class can be accessed internally, and subclasses of different packages can be accessed as well as other classes of the same package.

can inherit to subclass

Public

can be accessed anywhere

can inherit to subclass

Java Interview Fourth day

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.