What is a static method?

Source: Internet
Author: User

In C + +, if the method of a class is given the static keyword, the method is called a static method and the other is an instance method. Static methods are class-owned and can be used by objects or by classes. However, it is generally advocated to use the class name, since static methods can be used whenever a class is defined, without having to establish an instance of the class. Static methods can only use static members of a class.

Summary: An instance method must be used by an instance of the class. Instance methods can use non-static members of a class, or they can use static members of a class. Static variables of the class are loaded when the class is loaded. However, it is important to note that static variables of a class are common to objects of that class, that is, all objects share variables. Therefore, it is recommended to use as few static variables as possible. Try to use internal variables in static methods as much as possible.

Methods: Static methods, like static variables, belong to the class itself, not to an object of that class. To invoke a method that is defined as static, you must precede it with the name of the class. Instance methods must be used through an instance of the class. Instance methods can use non-static members of a class, or they can use static members of a class. Static variables of the class are loaded when the class is loaded. However, it is important to note that static variables of a class are common to objects of that class, that is, all objects share variables. Therefore, it is recommended to use as few static variables as possible. Try to use internal variables in static methods as much as possible.

Declaration: Where the Static keyword is represented statically. The syntax for declaring a static method is as follows:< access modifier >static Returns the type method name (parameter list). Inside the function body, you cannot define static variables, or you can define a final, static type variable outside of the function.

Call: A static method is unique to an instance method, which is a static method that adds the static keyword before the return type. There are two ways to invoke a static method:

(1) Call through the instance object of the class

The call format is: Object name. Method Name

(2) calling directly through the class name

The call format is: Class name. Method Name

Usage rules: We should note that static methods can only access static members of a class, cannot access non-static members of a class, have access to static members of a class, or access non-static members of a class, and static methods can be invoked either with an instance or by a class name. static method implementation rules. code example:
using System;namespace teststatic {classStatictest {intx; Static inty;  PublicStatictest (intAintb) {x=A; Y=b; } Public voidSimpleprint () {Console.WriteLine ("x=" + x + ", y=" +y); } Public Static voidStaticprint () {Console.WriteLine ("Y={0}", y); //Console.WriteLine ("X={0}", x);//non-static members cannot be used in static methods}}classTest {Static voidMain (String[]args) {Statictest St=NewStatictest (10, 23); St.  Simpleprint (); //St. Staticprint (); //static methods cannot use instances to invokeStatictest.staticprint (); }}}

What is a static method?

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.