Big Data Java Foundation Nineth Day

Source: Internet
Author: User
Tags class definition instance method

Features of 1.JavaBean:

    • Private member properties
    • Public Member method
    • There are get/set methods for assigning/taking values to member variables
    • Empty construction method

JavaBean that meet these conditions are called standard.

2. Construction method (also called constructor)

A constructor method (also called a constructor) is a special method that defines a location in a class, outside a member method, a member variable, a member method is a peer relationship, a definition of a constructor method and a generic member method, and it is characterized by the same name as the class, but with no return value, its syntax is as follows:

[modifier] Constructor Method name (formal parameter list) {

0 to multiple executable statements to form the execution body of the construction method

}

3. Considerations when constructing method definitions:

1. The construction method must have the same name as the class

2. The constructor does not return a value or void, and once the return value or void is added, the method is a member method, not a constructor.

4. Default Construction method

When defining a class, if no constructor is explicitly defined, as in the previous example, no constructor is defined, and the class is given a default parameterless construction method, and there are no statements in the constructor method.

For example:

Class demo{

Public Demo () {}

If no construction method is defined, the system automatically adds an empty parameter construction method

public void Show () {

System.out.println ("Hello World");

}

}

Once you have a custom construction method, the system does not add any construction methods.

Class demo{

public Demo (int id) {

System.out.println ("This is a method of constructing a parameter");

}

}

5. Use of construction methods

In fact, when you use the New keyword in a program to create an instantiated object for a class, you call the appropriate constructor for that class.

It should be noted at this point that different constructor methods are called depending on the parameter list of the constructor method, and the construction method that does not exist must compile an error.

6.this keywords

The This keyword is used to point to the current object, depending on where the This keyword appears, there are two cases of this as a reference to an object:

    • Referencing the object that the constructor is initializing in the constructor method
    • Referencing the object that is calling the method in the method

One of the usage scenarios for this keyword is to let one of the methods in the class access another instance method or variable in the class, for example, define a person class, which defines two methods, one called Sayhi () method, and another speak () method, in Speak () Method calls the Sayhi () method first, how do you define the person class?

Look at the following sample code:

Class person{

Public person () {}

Greeting method

public void Sayhi () {

System.out.println ("Hello");

}

Talking method

public void speak (String content) {

To use the Sayhi method to the person class, first create an object, and then call the object's instance method

Person p = new person ();

P.sayhi ();

SYSTEM.OUT.PRINTLN ("I Want to say:" + content);

}

}

Class persontest{

public static void Main (string[] args) {

Person per = new person ();

Per.speak ();

}

}

7. In the test class, first instantiate a person object per, and then call the object's Speak method, but when this method is called,

A Person object p is created, and then the Sayhi method of the object p is called, so that in the process of running the program, there are actually two object instance objects produced.

This program can implement method invocation, but is this good? The purpose of the person object created by the program in the Speak method is actually very simple, in order to invoke the Sayhi method of the object once.

8.static keywords

The literal literal translation of the static keyword is "static", which in fact is very vague and does not explain the real effect of static.

The real function of static is to distinguish between member variables, methods, inner classes, initialization blocks (later) whether the four members belong to the class itself or to an instance of the class.

In a class definition, static is equivalent to a flag that has a static decorated member belonging to the class itself, and no static decorated member belongs to the instance object of the class.

Therefore, in order to distinguish between these two different member variables, there are static modified member variable/member method, called Class variable/class method, no static modifier member variable/member method is called instance variable/instance method

A category diagram for variables in Java:

9.static decorated class member variables and member methods (i.e. class variables, class methods)

Static member variables (class variables) are classes that can be accessed using either a class name or an object name

Static member Methods (class methods) are classes that can be accessed using either a class name or an object name

As long as the class exists, the program can access the class variable, the syntax of the class variable access

Class name. Class Variable Student.name

As long as the instance exists, the program can access instance variables of the instance, accessing the syntax of the instance variable

Instance name. Instance Variable s.id

Of course, a class variable can also be accessed through an instance of the class, accessing the syntax of a class variable through an instance

Instance name. Class Variable Student.name/s.name

Features of 10.static Keywords

The part that is modified by the static keyword has the following characteristics

    • Load as the class loads
    • Precedence over object existence (because the loading of a class takes precedence over the creation of an object)
    • is shared by all instance objects of the class (existing in the static area of the method area, and if the variable is a class variable, any object that modifies it will be reflected in other objects), which is the criterion for determining whether to use a static keyword when defining a class
    • can be accessed by the class name (class name, static variable name/class name, static method name), or by using the object name, which is recommended for use with the class name because the static variable/method belongs to the class.

11. When do I use the static keyword? Criteria for using the static keyword

When a variable or method belongs to a class and not to an instance object of that class, you can use static to decorate it, so that you can use the class name. Member (variable/method) to access the static variable/method.

All instance objects of this class can see changes to static variables (class variables)

12.static Methods Key Considerations

    • The This keyword cannot be used in a static method (class method), which represents a reference to the current object, but the static keyword-decorated method belongs to the class and exists as the class is loaded, but when the class is loaded, the instance object is not yet created. The This keyword is not available
    • Static methods can only access static member variables and static member methods

13.static Memory Map

A variable or method that is modified by static has static extents in the method area

14.static keyword Considerations

Static keyword Considerations

A: Cannot use the This keyword in a static method

How to understand it?

Static is loaded as the class loads, and this is present as the object is created.

Because the analogy object first exists, it exists before the object statically.

B: Static methods can only access static member variables and static member methods

Static methods are accessed by:

Member variables: Only static variables can be accessed

Member method: Only static member methods can be accessed

Non-static methods are accessed by:

Member variables: can be static (because the class is already loaded, the member method can of course be called), or it can be non-static

Member method: But it is a static member method (because the class is already loaded), or it can be a non-static member method.

Easy to remember:

Static can only be accessed statically.

15. The difference between a static variable and an instance variable

belong to different

    • Static variables belong to classes, so they are also referred to as class variables
    • Instance variables belong to instance objects, so also known as instance variables

Different locations in memory

    • Static variables stored in a static area of the method area
    • Instance variables stored in heap memory

Different times when memory is present

    • Static variables are loaded as the class is loaded and disappear as the class disappears
    • Instance variables exist as objects are created and disappear as objects disappear

Different calling methods

    • A static variable can be called by a class name, or it can be called by an object
    • Instance variables can only be called by object name

Belongs

Memory location

Time of Occurrence

Call

static variables

Class

Static area of the method area

Load with class loading

The class name is called, and the object name is called

Instance variable

Object

Heap Memory

exists with object creation

Object Name Call

16. The difference between an instance variable and a local variable

Different positions in the class

instance variables in the class, and outside of the method

Inside a local variable method or on a method declaration (formal parameter)

In-memory locations are different

Instance variable heap Memory

Inside the local variable stack

Different life cycle

Instance variables exist as the object exists and disappear as the object disappears

Local variables exist with the invocation of the method and disappear as the call to the method finishes

Different initialization values

The instance variable has a default initialization value

A local variable has no default initialization value and must be defined before it can be used.

Declaration Location

Memory location

Life cycle

Initial value

Local variables

In the method, on the formal parameter

Stack memory

exists with method invocation

Must be defined, assigned, and then used

Instance variable

Inside the class, outside the method

Heap Memory

exists with object creation

has default initial value

The Main method is static

public static void Main (string[] args) {}

Public is called by the JVM, and access permissions are large enough.

Static is called by the JVM without creating objects, accessing the class name directly

Void is called by the JVM and does not need to return a value to the JVM

Main a generic name, although not a keyword, but is recognized by the JVM

String[] args was previously used to receive keyboard input

The format of the 18.main method is explained:

public static void Main (string[] args) {...}

Public: Common, access is the largest. Because the main method is called by the JVM, the permissions are large enough.

Static: Statically, no objects need to be created, by class name. Facilitates the invocation of the JVM.

void: As we have said, the return value of the method is returned to the caller, and the main method is called by the JVM. It doesn't make sense to return content to the JVM.

Main: Is a common method of entry. The language I have seen is the entrance to main.

String[] args: This is an array of strings.

This thing in order to receive data from the keyboard input.

The format is:

Java Maindemo Hello World Java

19. How to use the API documentation

Click Show, find index, see Input box

Take scanner Example

Type scanner in the input box and enter

Look at the bag

The classes under the Java.lang package do not need to be imported, and all others need to be imported.

To import:

Import Java.util.Scanner;

A simple look at the explanation and description of the class

Look at the structure of the class

Member Variable Field summary

Constructor method Construction Method summary

Member Method Method Summary

Learn how to construct

A: Create an object with a construction method

B: No constructor method members may be static, and such classes do not need to be constructed, directly using the class name.

See Member methods

A: Left

Static: If static, can be called through the class name

Return value type: What is returned, you receive with what.

B: Right

Look at the method name: method names don't write wrong

Parameter list: What you want, what you give, how many people want, you give a few

Big Data Java Foundation Nineth Day

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.