Creating and using 06 objects

Create Object

v      To obtain a class object requires two steps:

§ You must declare a variable of the class type, this variable is not defined an object. In fact, it's just a simple variable can reference objects.

§ The statement to create an object of actual physical copy, and put a reference to the object is assigned to the variable.

Statement object

v      statement by "class name variable name;" statement to achieve

When stated object is empty (i.e., null); In this case the object can not be used; the use of such type of error will be reported NullPointerException;

Allocate memory

Allocating memory by the keyword "new + class constructor ();" statement to achieve

v      allocate memory allocation is the process entity (variable) to the object; assign several variables are allocated and what is determined by the member variable created the class;

v      The member variables in the class is not initialized, after automatically initialized with default values assigned to the object variable (create an object); default Boolean false; default integer 0; Float default 0.0; default class type null;

Use objects

v      Objects not only have properties of their own, it also has created a class method;

v      objects by operator " . visit their properties, their method of calling";

 

v      pass a reference value transfer

Parameter passing

v      When the method is invoked, if the parameter, the parameter need instantiated (initialization); i.e. specific parameter values;

v      is the value passed when the java, the basic parameter data type; class types are passed by reference;

The difference between class variables and instance variables

v      objects are created by the class; thus required to load the appropriate class object creation time; and a plurality of class objects can be created (by the new + constructor), each object has its own entity (variable);

v      difference:

§ class variables are allocated at the time the class is loaded memory; instance variables allocated memory when you create an object; class variable release the memory when the program exits; object does not exist instance variables do not exist

§ Class variables are shared by all objects that share the same class of all objects in the class of such variables; instance variables unique to each object; operating its own instance variables without affecting other objects

§ class variables are generally invoked by class name;

§ class static variable declared

The method and the class distinction Examples of methods

v      when the class is loaded, the class entry is allocated address method; examples entry address is allocated when the first object is created

v      class method is generally invoked by class name

 

v      class keyword this method can not be used; for this representative of the current object; class loading method when there is a time when the class object does not exist

Garbage collection (garbage collection)

v      memory release

       Java uses an automated process to re-allocate memory approach: garbage collection: When there is no reference to an object, the object is considered no longer needed, it occupies the memory will be freed.

   

v      If you want to force garbage collection, you can call

              System.gc();

finalize () method

v      is mainly used to release non-java resources, such as file handles or window font characters

v      method Prototype:
protected void Finalize () {
  // finalization code here Wallpaper
}

v      When the object is released, it will automatically call the object's finalize () method

Static blocks Features

v      Static blocks Features

§ static block (java statements all static blocks) executed when the class is loaded;

§ static block generally performed only once.

Target block: class is instantiated when executed, executed before the constructor.

Points added:

 

1: value passed with reference parameters passed

       A: value is passed: the basic data transfer types are passed by value

       B: reference transfer (transfer address): object data types are passed by reference.

 

2: Class variables and member variables (instance variables, object variable)

 

       Class variables: by calling the class name, class variable is shared by all instances.

       finalstatic int MAX = 20; // Java defined constants

       Object variables: the object by calling (new object must come out).

 

3: Method with members of a class method (Method instance, an object method)

      

       Class methods: by calling the class name, you can not use this keyword in class methods. Because this represents the current representative.

       Member methods: by calling the object (the object must be new out).

4:

  Construction method

  Destruction methods (finalize)

       Destruction method is called when the object is destroyed.

       When an object is not a clear reference to it when the heap, Java virtual machine the object is considered useless.

 

  The garbage collector for recovering the object heap allocation. The garbage collector will only recover three times

  Memory. Garbage collector virtual machine is called automatically. (Heap memory is not enough under the circumstances call)

 

  But can be forced to run the garbage collector by System.gc ().

 

5: static target block static block

 

Looking main method ---> --- load the class> static block code is loaded classes (initialization only once)

 

---> static methods and static variables (initialized only once) ----> target block based method of load

 

---> constructor ---> call the object's method ---> object destruction methods perform object.

 

==== fast static

  java often have some static blocks, which is used for initialization before generating class, regardless of further java static in C ++, it is first initialized good.

So pay attention:

a static method can only be called directly in other similar static member (including variables and methods), but can not directly access non-static class members. This is because, for the non-static methods and variables, you need to create an instance of the class before use, and static methods do not create any objects before use.

b static method can not refer to this and super keywords in any way, since static methods do not create any instance of the object before use when a static method call, the object referenced by this did not produce,

For non-static blocks are present in each class, in addition with a static block that was first performed, (before the constructor)

 

 

In either case we have to use this keyword.

1. We want the constructor assignment outside the parameters passed to the member variable, the variable name and the same form of argument constructor of the class members, then you need this keyword to distinguish between local variables and class variables;

2. The construction method is automatically called up when an object, we can not call other methods like, like to call the constructor in the program but we can call other overloaded constructor in a constructor, the name is not a constructor, form but this (parameter list), wherein the parameter list according to, select the appropriate method of construction.

 

==========

finalize method is a method of the Object class, any class inherits this method from the Object that. This method only when the system to call the garbage,

But not all the garbage is recycled so not finalize method of each object are invoked;

 

================

Unlike the constructor has three points method characterized by:

1. It has the same name with the class name

2. It does not return value comprising

3. It does not return a value with the return statement

Another point to note is this: it does not return a value, but there is no void; if there is a void that it is no longer a constructor.

------------

--------------

The class constructor is like a imperial envoy sent by the emperor, like, it's just work to help the emperor, but in the outside world he represented the emperor, so the constructor and the class as it's name. But imperial emperor may have only a few.

 


Reproduced in: https: //my.oschina.net/u/2552902/blog/543823

Guess you like

Origin blog.csdn.net/weixin_33994444/article/details/92326539