The role of static in Java learning notes

Source: Internet
Author: User

The role of a member in a class is modified by the static:

1. Members (member variables and member methods) that are static decorated within a class can be called directly from the class name.

2. Variables in a class that are modified by static are the same variables that are used in all of their objects.

As in the following example:

class person
{
static int Age ;
Static
{
System. out . println (" Static code block");
}

Public static voidMain(String[] args)
{
PersonP1=NewPerson();
person p2 = new person () ;
p1. Age = 10 system. out .println (p2. age ) }
} /span>

?

3. In a static modified member method, a non-static member variable cannot be called and cannot be called with this.

4. Static code blocks are modified using static, and static blocks of code are executed when the class is loaded.

The role of static in Java learning notes

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.