Introduction to the second season of Java first chapter classes and objects

Source: Internet
Author: User

What are classes and objects


How to define a class in Java


How to use objects in Java


member variables and local variables in Java
1. Member variables: defined in a class that describes the components that make up the object.
2, local variables: In the method of the class, to temporarily save the data.
3. Valid range of member variables: All methods in a class can call member variables.
4, local variables and member variables allow duplicate names, this time local variables have a higher priority.


How to construct in Java
1, if you do not write their own method of construction, Java will default to a non-parametric construction method. However, if the constructor is customized, the parameterless constructor given by the system will fail, regardless of the parameters.
2. When writing the constructor, you can check the rationality of the parameters by the way.
3, previously saw a description of the constructor: the constructor is the new moment of execution code, not necessarily used to initialize the object.


Static variables used in Java
1. Static variables are class-owned and shared by all objects of the class.
2, can be accessed with objects, but it is strongly recommended to use the class name Access .


Static methods for using static in Java
1, static methods can be directly called in the same class of static members (methods), but not directly call non-static members (methods).
2. If you want to invoke a non-static variable (method) in a static method, you can access the non-static variable (method) by creating the object of the class and then through the object. For example: in the main () method.
3, in the ordinary member method, you can directly access the same kind of non-static variables and static variables.


Static initialization block used in Java
1, static initialization block only executes when the class is loaded and executes only once (and is executed first), while static initialization blocks can only assign values to static variables and cannot initialize normal member variables.
2. Instead of static initialization blocks are executed once each time new.
3. initialization blocks always take precedence over constructors .

Introduction to the second season of Java first chapter classes and objects

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.