Chapter Nineth Objects and classes

Source: Internet
Author: User

Object and class one object and class
    • A class is a mold that determines the characteristics (attributes) and behavior (methods) that an object will have;
    • A class is a set of objects of the same property and method;
    • Class is the type of object;
1. Properties

A specific value or feature.

2. Methods

The action that the object performs is what it can do.

3. Defining classes
    1. Java programs use class classes as organizational units;
    2. Composition: Properties and methods;
4. The difference between a member variable and a local variable
    • different scopes;
    • The initial value is different; Java gives the member variable default an initial value, while the local variable does not;
    • Local variable names with the same name are not allowed in the same method, but can be in different methods;
    • The nearest principle; The local and the member have the same name, the value of the local variable is taken precedence;
5. Construction method
    • Create an object using the new + construct method, which is the constructor of the calling class ;
    • The constructor method name has the same name as the class and no return value;
    • The construction method can also be overloaded;
6. Static modifier

You can modify variables, methods, and blocks of code. It is used by all objects of the class, and when the JVM first uses the class, it allocates memory until the class is unloaded for resource reclamation.

    • Static methods can call static members directly in the same class, but cannot call non-static members directly;

    • In the ordinary member method, the same non-static variables and static variables can be accessed directly;

    • Non-static methods cannot be called directly in static methods, and non-static methods need to be accessed through objects.

    • 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; View the following code:

Operation Result:

by outputting The result, we can see that the static initialization block is executed first when the program is run, then the normal initialization block is executed, and the construction method is finally executed. 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.


Hiding some information about a class inside a class, not allowing direct access to external programs, but using the methods provided by the class to manipulate and access the hidden information.

1. Access modifiers

2. This keyword


Chapter Nineth Objects and classes

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: 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.