Class Members and Methods

Source: Internet
Author: User

Data Member: Data members can be dividedStatic Member,Instance Member
Static Member: Static member variables are associated with the class and can be used as "common" variables in the class. They are not dependent on the existence of characteristic objects.Class Name adding operators and variable names for access
Instance Member
: Instance member variables are associated with objects, and access to instance members depends on the existence of instances.

Function member (static method, instance method)
Static Method
: Static methods do not belong to specific objects. Static methods can access static member variables,Static methods cannot directly access instance variables.,Static methods cannot directly call instance methods.Can be called indirectly. First, you need to create an instance of the class, and then call the instance method through this specific object, but you can call the static method.
Instance method: The execution of an instance method is associated with a specific object. Its execution requires the existence of an object,The instance method can directly access static variables and instance variables. The instance method can directly access the instance method and static method.
The access method of static methods adds variable names to some class name operators.
When multiple instance objects exist, the memory does not copy each specific instance method, but all objects of the same class share a copy of each instance method.

Class Test
{
Int X;
Static int y;
Void F ()
{
X = 1; // correct, equivalent to this. x = 1
Y = 2; // test. y = 1
}
Static void g ()
{
X = 1; // error. You cannot access this. x static constructor. You can only access static members.
Y = 1; // correct
}
}
Static void main ()
{
Test T = new test ();
T. X = 1; // correct
T. y = 1; // error. You cannot access static members in a class instance.
Test. x = 1; // error. You cannot access non-static members by class.
Test. y = 1; // correct
}
}

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.