Object-oriented (static keyword)

Source: Internet
Author: User

/**
* Created by rabbit on. Blog
* 1. static: static
* Usage: it is a modifier used to modify members (member variables, member functions ).
* When a member is statically modified, an additional call method is provided, except that the member can be called by an object,
* It can also be called directly by the class name. Format: Class Name. static member
*
* 2. static features
* 1. Loading with classes
* That is, the static state disappears with the disappearance of the class. It indicates that his life cycle is the longest.
*
* 2. takes precedence over object existence
* Static objects exist first and objects exist later.
*
* 3. shared by all objects
* 4. It can be directly called by the class name.
*
* 3. Differences between instance variables and class variables
* 1. storage location
* Class variables exist in the method area as the class is loaded,
* Instance variables exist in heap memory as objects are created.
* 2. Lifecycle
* The maximum lifecycle of a class variable disappears as the class disappears.
* The lifecycle of an instance variable disappears as the variable disappears.
*
* Iv. Static usage considerations
* 1. Static methods can only access static members. Non-static members can access static members,
* You can also access non-static members.
* 2. this and super keywords cannot be defined in static methods.
* Because the static state takes precedence over the object existence, the this and super keywords and,
*
* 5. Static advantages and disadvantages
* Benefits: the shared data of objects is stored separately to save space. You do not need to store one copy of each object.
* Can be called directly by Class Name
* Disadvantages: the lifecycle is too long,
* The usage has limitations. Static methods can only call static variables;
*
* Created by rabbit on. Blog
*/
Public class static_demo
{
String name; // member variable (instance variable)
Static String country = "CN"; // static member variable (class variable)
Public static void show ()
{
System. out. println (country + "method ");
}
}
// Created by rabbit on. Blog
Class demo_static
{
Public static void main (String [] args)
{
System. out. println (static_demo.country );
Static_demo.show ();
}

}
// Created by rabbit on. Blog

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.