java trivial knowledge base

 

1, java variable types

Class variables (static variables): Use the static keyword to declare in the class, in the constructor, method, statement blocks away. No matter how much a class creates objects, classes, class variables only have a copy. Static variables beginning of the program created, destroyed when the program ends. It can only be called a static method. Similarly to Example variables have initial default values. It can be called directly by ClassName.ValueName.

Examples of variables: a statement in the constructor, method, or blocks other than statements. In the instance variables when the object was created to create, destroy, when the object is destroyed. Instance variable is referenced at least a constructor method, or block. Examples of such external variable value can be obtained by these means. Instance variables can be declared before the reference, you can also declare after the reference. Instance variable is the default value, the value 0 is the default type, default Boolean is false, the default reference type is null. Examples of variables can be modified using access modifiers.

Local variables: variables declared in the constructor, which statement block. In the method of execution or when the statement block creation, will be destroyed after the implementation. Access modifier can not be used to modify local variables, they are created on the stack. There is no default, so be sure to use initialization.

Static variables belong to the class, the class does not generate the object can be called a static variable through the class name. As long as the program is loaded the class bytecode enough space is allocated

Class instance variables belong to the object, the object can only generate calls instance variables. We must create an object, only to allocate space

Static method call only static variables. Non-static method can call static variables can call the instance variables.

Static data members are shared by all objects created, any object access static variables used are the same data, the same piece of memory. No matter which object to modify the static variables of other objects, the static variable has changed.

2, java of modifier

1) access modifier

The default access modifier : can be used to modify classes, interfaces, methods, variables. The default class, this package can only be accessible to other classes. The default interface method for public access type, interface variables public static final.

Private access modifier --private: can only be used to modify variables, methods, constructors. Can only be modified using a private visit their class. private external classes and interfaces can not be modified. This variable is used to protect the variables and methods not want to be accessible to the outside. Operating variables can be modified by modifying private get, set method provided.

Public access modifier --public: can be modified classes, variables, constructor, method

Protected access modifier --protected: can not be used to decorate the outside classes, and interfaces. Inner classes, variables and methods can be modified. Internal modified classes, variables and other types of methods can be invoked in the present package.

protected internal modified classes, methods, variables, this package can only be invoked in other classes, inheritance within the class is called a subclass.

The method of a class has been protected modified, temporarily referred to as the base class class. With the base class in the same package a subclass of the class whether or not in the base class, can access to the protected modified method by instantiating objects instantiated class or a group of the subclass of the base class. Not in the same class and the base package subclass of the base class can instantiated protected access method for modifying super keyword or by subcategory to. Not in the same class and the base package is not a subclass of the base class is modified, there is no way to call a method in the base class is modified to be protected.

In the parent class is declared as protected method in a subclass can be life for the public or protected, can not be declared: private

2) non-access modifiers

Extend knowledge: the role of the constructor is to create objects and object initialization. The constructor runs when the object class created a common object calls the method when it is implemented, the constructor is only executed once, the number of common method of execution depends on the call object.

Subclass inherits the parent class, subclass object is initialized when the constructor of the parent class also needs to be performed. Because subclasses of the parent class using this property display or implicit call the parent class's constructor. Subclasses always begins with at least one constructor super (), may be implicit; at least this is not the beginning of a constructor subclass () ;. The constructor may call subclass of this class constructor, an indirect call the constructor of the superclass.

the this (); or Super (); objects are constructed of a type specified in jvm virtual machine. Call a constructor in two other constructors represents the creation of two objects, so the provisions of this (); or super (); need in the first line of the constructor.

When other constructor calls this constructor class, the this (); or Super (); in the first line are required constructor, so a constructor can be called a constructor further.

this keyword: this (); indicates the current class's constructor, this method can only be placed in the first row constructor, to ensure that before any action, object has been initialized, constructor can only be called a constructor, to ensure that only objects initialization time.

Instead of using this method in general (); avoided because of the operation of the object, the object itself has not been successfully constructed. If you want to call the constructor method in general, you can use a new object.

When the member variables and local variables duplicate names, when used in this process, it is represented by a member variable of the class where the method (this represents the current object itself). When no local variables subject to this

When passing himself as a parameter, you can use this (this as the current parameters are passed)

Encountered an internal class or anonymous class within the class or the use of anonymous class this indicated that the anonymous inner class or the class itself. If you want to invoke a method or variable outside the class can: External class name method name (static method), outside the class name .this method name (common method)

static modifier: static method that is not of this. this represents the objects, static variable or method in the class is loaded when it was loaded, loaded before the object created. If this object is placed in a static method compiler will find this on behalf of, but did not create an object, you can not find the error.

represents a modified static class variables or class method, you can not use local variables static modification.

Can not access non-static variable in a static method, since non-static variables are part of the object, static method exists before creating the object.

The final modifier: final modified variables can only be assigned once, duplicate assignment error. final modification of the object can not be modified, but the value of the object variables can be modified. The modified final method can be inherited by subclasses, but can not be modified by subclasses. The modified final class can not be inherited.

abstract modifier: use modified abstract class is an abstract class, can not instantiate an object, a class can not be modified using the final and abstract, because the final modified class can not be inherited, but the abstract class can inherit only be instantiated, to be useful. Two are in conflict.

An abstract class may contain a non-abstract methods and abstract methods. However, the abstract class contains a method, it must be an abstract class. Because the method is not overridden abstract of no use, it is no entity. Concrete implementation of the abstract method provided by subclasses

 

synchronized modifier: modified method to be synchronized at the same time can only be one thread access.

The volatile modifier: volatile modified member variable is accessed every time the thread will re-read value of the variable from memory. When a member variable changes, the forces of change in the value of the written back to memory, so all the threads see the variables are the same value.

The final modifier can not be used before volatile, because the final modified variables can not be modified.

variable parameters java method: in the process of having a variable length parameter may be used as an array. The method of fixed parameters and how to match the call issued, it is possible matching and variable length, the fixed priority method and parameters match. Variable-length parameter of method parameters on the final surface, because if an enlarged front, do not know when the corresponding parameter is fixed. Each method can have a variable length parameter, the parameter is consistent with the method of variable length into the final surface.

public static void main(String[] args) {
        VarArgsTest1 test = new VarArgsTest1();
        test.print("hello");
        test.print("hello", null);
    }

Do not let the threat of a null value to the parameter variable length, do not hide argument types

 public static void main(String[] args) {
        VarArgsTest1 test = new VarArgsTest1();
        String[] strs = null;
        test.print("hello", strs);
    }

 

 

 

Published 15 original articles · won praise 0 · Views 1078

Guess you like

Origin blog.csdn.net/ma316110/article/details/80250938