Java Classes and objects

Source: Internet
Author: User
Tags float double modifiers

    • The composition of the class:
    1. Property (member variable, Field).
    2. Method (Member method, function, method).

Properties : member variables VS local variables

Same point: 1. Follow the format of the variable declaration-data type variable name = Initialize value

2. There are scopes

Different points: 1. The location of the declaration is different: the member variable is declared in the class, the method is outside the local variable: declared within the method, the formal parameter part of the method, or the code block.

2. There are four modifiers for member variables: Public private protected default. Local variables have no modifiers, same as the method modifiers in which they are located.

3. Initialization value: There must be an initialization value

Member variables: If the assignment is not displayed at the time of declaration, then different data types will have different default initialization values (byte short int long initialization value is 0,float double initialization value is 0.0)

(Char initialization value is a space, Boolean is false, the reference data type Initializes a value of NULL).

Local variables: Assign values that must be displayed. (Local variables do not have a default initialization value).

4. The two locations in memory are different: the member variable is stored in the heap space, and the local variable is stored in the stack space.

Method: provide an implementation of a function

1. Example public void Eat () {//Method body}

Public String GetName () {}

public void SetName (String n) {}

Format: Permission modifier return value type (void: no return value/specific return value) method name (formal parameter) {method body}

2. About return value type: void: Indicates that this method does not require a return value

A method that has a return value: At the end of the method there must be a variable of return+ return value type

3. Other methods or properties in this class can be called within a method, but cannot be defined within a Method!

    • The principle of object-oriented programming in thought

1. Design and create components of class and class

2. Instantiating an object of a class

3. Complete a function in the form "object. Properties" or "object. Method"

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