"Java Starter series" Static keyword

Source: Internet
Author: User

The static method is the method without this. A non-static method cannot be called inside a static method, which in turn is possible. It is also possible to invoke the static method only through the class itself, without creating any objects. This is actually the primary use of the static method. The idea of Java programming

static variables

Static-modified variables are called statically member variables, also known as class variables, and class variables have the same life cycle as classes and are valid throughout the execution of the application.

The difference between a static variable and a normal variable: a static variable is shared by all objects, only one copy in memory, only when the class is initially loaded, the non-static variable is owned by the object, it is initialized when the object is created, there are multiple replicas, and the replicas owned by each object do not affect each other.

Static methods

1, the static method of modification is called statically, because the static method is not dependent on any object can be accessed, so for static methods There is no this, because it does not attach to any object, since there is no object, it is not to mention this. 2. Static methods cannot access non-static member variables and non-static member methods of a class because non-static member methods/variables must be dependent on specific objects to be called, but static member methods/variables can be accessed in non-static member methods. 3. If you want to invoke a method without creating an object, you can set this method to static. The most common static method is the main method, and as to why the main method must be static, it is because the program does not create any objects when it executes the main method, so it is accessible only through the class name.

"Java Starter series" Static keyword

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.