Third day in Java

Source: Internet
Author: User

The constructor is in memory:

1 classConsdemo2 {3      Public Static voidMain (string[] args)4     {5Person p=NewPerson ("Zhang San", 12);6 P.sayhello ();7     }8 }9 Ten class Person One { A     PrivateString _name; -     Private int_age; -      PublicPerson (String name,intAge ) the     { -          This. _name=name; -          This. _age=Age ; -     } +      Public voidSayHello () -     { +System.out.println ("My name is +_name+", I'm "+_age+" this year.); A     } at}
Person p= Thenew person ("Zhang San", "n");p. SayHello (); 1. Main function into the stack, open a space in the heap, Name,age assigns the initial value 2. The person () constructor is in the stack and assigns the value to P object 3. Person () stack 4. SayHello () into the stack, call function 5.SayHello () stack

This: is the reference to the object to which the function belongs.

This can also be used to call other constructors in the constructor, note: Only the first row of the constructor is defined. Because the initialization action should be implemented first.

The characteristics of static:

1,static is a modifier used to decorate a member

2,static decorated members are shared by all objects

The 3,static takes precedence over the existence of the object because the static member already exists as the class is loaded.

The members of the 4,static modifier are called in a way that can be called directly by the class name. Class name. Static member

5,static decorated data is a shared resource, and the data stored in the object is unique

The difference between a member variable and a static variable:

1. Two variables have a different life cycle

Member variables exist as objects are created and released as objects are recycled.

Static variables exist as the class loads, disappearing as the class disappears.

2. Different calling methods

Member variables can only be called by an object

A static variable can be called by an object, or it can be called by a class name

3. Different aliases

Member variables are also known as instance variables

Static variables are also known as class variables

4. Data storage location is different

Member variable data is stored in the object of the heap memory, so it is also called the object's unique data

Static variable data is stored in the static area of the method area (also called the data area, shared area), so it is also called the shared data of the object

Considerations for static use:

Static methods can only use static members

The this or Super keyword cannot be used in a static method

The main function is static

The main function is special:

public static void Main (string[] args)

The format is fixed

Identified and invoked by the JVM.

Public: Because the permissions must be the largest

STATIC:JVM does not need an object, it can be called directly with the class name of the main function.

void: The main function does not have a specific return value

Main: function name, not keyword, just a fixed name for the JVM to recognize

String[] args: This is the argument list of the main function, the parameter of an array type, and the element is a string type

static invocation:

1, static variable

The values of the member variables that are available in the Analysis object are the same. This is the member that can be statically modified.

As long as the data in the object is different, is the object's unique data, must be stored in the object, is non-static.

If the same data, the object does not need to make changes, only need to use, do not need to store in the object, defined as static.

2, static function

Whether a function is statically decorated, it is a reference to whether the function has access to the unique data in the object.

Simply put, from the source code, whether the function needs to access non-static member variables, if necessary, the function is non-static. If not required, it is static, of course, can also be defined as non-static.

But non-static needs to be called by the object, and only the creation of the object calls non-static methods that do not have access to the unique data, the creation of the object is meaningless.

Static code block

Static {    System.out.println ("hahaha");          }

Executes as the class loads

Function: Used to initialize a class

Constructing blocks of code: You can initialize all objects

{      system.out,println ("hahaha");  }

The program is invoked when the memory is displayed:

Third day in Java

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.