Differences between class variables and instance variables

Source: Internet
Author: User
A class variable is also called a static variable, that is, a static variable is added before the variable;
Instance variables are also called object variables, that is, variables without static;
The difference is:
The difference between a class variable and an instance variable is that a class variable is shared by all objects. One object changes its value, and other objects get the changed result; instance variables are private objects. A certain object changes its value without affecting other objects;
Example:
Public Class {
Static int A = 0; // class variable
Private int B = 0; // Strength Variable
}

Public Class B {
Public void main (string [] ARGs) {
A a1 = new ();
A a2 = new A ();
a1.a = 3; // equivalent to. A = 3;
a1. B = 4;
system. out. println (a2.a); // The result is 3
// class variable is for all objects, so A1 changes a and A2's a also changes
system. out. println (a2. B); // The result is 0
// The instance only changes its own, so B of the A1 object changes, variable B of object A2 is not affected
}< BR >}

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.