Java Basics-Classes and objects

Source: Internet
Author: User

Java class and Object learning note I, member variables and local variables (1) are defined

1. Member variables

defined in the class to describe what the object will be. ( the default member variable value is 0)

2. Local Variables

Defined in a method of a class to temporarily save data in a method.

Demo Example:

  

(2) The difference between a member variable and a local variable

1. Different scopes

The scope of a local variable is limited to the method that defines it , and the scope of the member variable is visible within the entire class .

2. Different initial values

Java gives the member variable an initial value ( the initial value is 0), and Java does not assign the initial value to the local variable. (The use of local variables must first be assigned value )

Example:

    

3. Case of the same name

Local variables with the same name are not allowed in the same method, and local variables of the same name can be used in different methods.

Example:

   

4. Priority level

When two types of variables have the same name, local variables have precedence.

  Example:

  

Operation Result:

  

Second, the construction method

1. Create a new object using the new+ construction method.

2, the construction method is defined in the Java class to initialize the object method (the other method is used to represent the action of the object, the meaning of the construction method is special)

3. The constructor method has the same name as the class and has no return value .

The format is as follows:

  

4. The construction method without parameters is automatically generated when the object is initialized. (You can override this method to implement your own parameterless construction method.)

5, the use of the method with the parameter construction (can coexist with the non-parametric construction method, because it is an overloaded function)

Example:

  

Note the three points:

1, without specifying the construction method, the system will automatically add an argument-free construction method, when there is a specified construction method, whether the method is a parameter, no argument, no parameter is automatically added to the construction method.

2, the construction method of overloading : The method name is the same, but different parameters of multiple methods, the call will automatically choose the appropriate method according to different parameters .

3, the construction method can not only assign values to the object's properties, but also guarantee to assign a reasonable value to the object property.

Example:

    

Third, static use in Java

As we all know, we can create multiple objects of this class based on a class, each with its own members and independent of each other. At some point, however, we would prefer all objects of the class to share the same member . This is the time for static!!

Use static to modify variables, methods, and blocks of code .

1. Static variables

Members in Java that are decorated statically are called static members or class members . It is owned by the entire class , not by an object, which is shared by all objects of the class.

Here are three points to note when we use static variables:

1. All objects of the class share the same member .

2. Static members can be accessed directly using the class name (recommended)or by using the object name.

3. Static members belong to the entire class, and when the system first uses the class, it allocates memory space until the class is unloaded for resource recycling!

Example:

    

Operation Result:

      

2. Static method

As with static variables, we can also use the static modification method, called a static method or a class method . In fact, the main method we've been writing about is static methods. Static methods, like static variables, can be used directly using the class name (recommended), or can be used with the object name, which is no longer interpreted too much.

There are three points to note:

1 static members can be called directly in a static method, but not non-static members directly . Such as:

    

If you want to invoke a non-static variable in a static method, you can access the non-static variable by creating the object of the class and then through the object . Such as:

    

2, in the ordinary member method, you can directly access the same kind of non-static variables and static variables , as follows:

    

3, static methods can not directly invoke non-static methods , you need to access the non-static method through the object . Such as:

     

The above three points look very complex, in fact, it is simple to understand: the use of ordinary member methods and member variables must be called through the object, the object is the instantiation of the class, and static member methods and member variables can be directly through the class (do not need to instantiate the object) can be used. So when a static member method is to use normal member methods and member variables, it cannot be called directly (because object instantiation is required and static is not required), whereas the normal member method can directly access the same non-static variable and static variable (because it has been instantiated).

3. Static initialization block

In Java, data can be assigned by initializing blocks. Such as:

    

In the declaration of a class, you can include multiple initialization blocks, which are executed sequentially when you create an instance of the class. If you initialize a block with the static adornment, it is called a static initialization block .

It is important to note that static initialization blocks are only executed when the class is loaded and only once , while static initialization blocks can only assign values to static variables and cannot initialize normal member variables .

Let's look at a piece of code:

    

Operation Result:

    

Analysis: Through the output, we can see that the program runtime static initialization block is executed first, and then execute the ordinary initialization block, the final execution of the construction method. Because static initialization blocks are only executed once when the class is loaded, static initialization blocks are not executed when the object Hello2 is created again. (The order of the three is important).

Meaning: The initialization block is the addition of the constructor, the initialization block is not able to receive any parameters , the definition of some of the common properties of all objects, methods and other content can be initialized with the initialization block!! The benefit is that the initialization block can be reused to improve the maintainability of the entire application.

Note: This article is based on my lessons online Java course Summary, interested in the small partners can go to the network for video learning and online programming, faster and better understanding of knowledge points Oh ~ Welcome to the small partners at any time to consult the knowledge point φ (>ω<*)

  

  

Java Basics-Classes and objects

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.