Java programming language (version 4th) Reading Notes (2)-classes and objects

Source: Internet
Author: User

The class has three types of members: field, method, and nested class and nested interface. The learning class can be expanded from the Declaration, modifier, initialization (including Class Object creation) and usage of the three members. Here we mainly talk about fields and methods.

Statement

Field Declaration: modifier (optional), type name, field name, initialization clause (optional );
The method declaration is modifier, return type, method signature (method name, parameter list), exception, and method body.

Modifier

The table is summarized as follows:

Each modifier can be expanded into a very large topic. In the table, "yes" indicates yes, and "no" indicates no.

Initialization

The most direct way to initialize a field is to use the initialization clause. Field initialization clauses can be constants, other fields, and call methods. Note: (1) the initialization clause must ensure that the type is correct; (2) the initialization clause cannot throw an exception. Fields that are not initialized are assigned the default initial value.

Object initialization. Initialization must be displayed. The initialization method is the constructor (called the constructor in the book) and the initialization section (in the class, except for fields, methods, nested classes, and nested interfaces, or use the code segment included in static {}, which is also called static initialization. Obviously, only static data members can be initialized ).

Use

Here, I understand that the methods in Java are passed by value without being passed by reference. The sample code for a test is as follows:

 1 class A 2 { 3     int v = 0; 4 } 5  6 class B  7 { 8     public static void main(String[] args)  9     {10         A a = new A();11         System.out.println(a.v);12         foo(a);13         System.out.println(a.v);14         foo2(a);15         System.out.println(a.v);16     }17 18     public static void foo(A a)19     {20         a.v = 1;21     }22 23     public static void foo2(A a)24     {25         a = new A();26         a.v = 2;27     }28 }

If the output value is 0, 1, and 2 (displayed in three rows, the same below) according to the previous reference transmission viewpoint, the actual output value is 0, 1, and 1. This shows that the parameter A passed to foo2 is still a copy, which means passing by value.

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.