Final, static (Java), const, static (C #)

Source: Internet
Author: User

Now let's look at the following equivalence relationships:

Static in C # and static in Java

Simple. The two are used in the same way. The following two aspects are discussed:

1.Variables belong to the class, not the instance level. It can only be called by class name, but not by instance.

2.If the value is assigned during definition, the assignment of all static variables is completed first during class initialization. However, the initialization sequence of all static variables cannot be determined.

Const in C # And finnal in Java

For a long time, I have always thought that the two functions are the same, but they cannot be changed after variable initialization, that is, they can only be assigned values at the time of definition or in the constructor. However, this is only one-sided. We will analyze it in detail below:

1. Modify Variables

To be accurate, const in C # is equivalent to static final in Java. That is to say, final in Java does not have static functions. Const in C # has the static function. Therefore, in C #, public static const string will be set to public const string.

2. modifier classes and Methods

In this case, final in Java is similar to sealed in C #. That is to say, the final-modified class cannot be inherited, and the final-modified method cannot be overwritten.

Const in C # Cannot modify classes and methods.


1.Role of Private Static members (Private static variable)

It is private, and cannot be used outside the class; static, global variable. It seems very contradictory and cannot be used outside the class. What is the global use. Well asked, the global class is also very meaningful, such as private static int A = 5, then, it can be ensured that variable A will be preferentially initialized during class initialization (before the constructor is executed ). In this way, if the initialization of object A requires an instance of object B, you can use this declaration to ensure that the instance of Class B can be used in the constructor of Class. At the same time, private can ensure that instances of Class B can only be used in Class A, which plays a very good role in sealing.

2.Private final Member)

Before the class constructor is complete, the member must be initialized. Once defined, the member cannot be changed. The member can only be used in this class. Instances and subclasses cannot be used.

Members modified by Private Static final are assigned a value during the Declaration to ensure that they can be used in the constructor. A member modified by Private Static final usually represents an instance of other components, and the variable is the global variable in the class.

The private final modified member is assigned a value in the constructor, indicating that it is a global private member variable of the class. The constructor needs to input their initial values to complete class initialization.

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: 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.