Java Fundamentals---"Static members, encapsulation, package"------March 6, 2015

Source: Internet
Author: User
Tags modifier

Why does the main function of a class in Java () need to be added static?

Explain:

Let's talk about static.

1.static decorated domain, we call static domain, it is classified all, is shared by all objects, only one
2.static modified chunks, fields are initialized only once
3.static Modified field or method, can be directly by the name of the class plus. Making Calls
Non-static members cannot be used directly within a 4.static decorated method

The static keyword tells the compiler that the main function is a static function. In other words, the code in the main function is stored in a static store, and the static method is fixed in memory, that is, when the class is defined, the code already exists. If the main () method does not use the static modifier, then the compilation will be error-free, but if you attempt to execute the program you will get an error indicating that the main () method does not exist. Because the class that contains main () is not instantiated (that is, an object without this class), its main () method does not exist. Using the static modifier means that the method is static and does not need to be instantiated for use.

The Main method is the entry of a program, if written as non-static, then you have to instantiate an object to call it, since it is the entrance is certainly not possible, the static method belongs to the class, directly with the class name can be called

The static method does not need to create objects that can be accessed directly so that the main method does not require special handling

The static method is in the quiescent data area so that it can be called directly without having to regenerate each call.

Static functions are not instantiated when they are invoked

Static, loaded into memory as soon as it comes up
The main function is actually a method of the same class, such as a class is Test, then the main of the class is actually Test.main (string[] args), it is well known that if a method is not static, you should first instantiate the class, such as to test t=new Test (); Before you can call Test.main ();
This is impractical for running a program's main function, so for example, define the main function as static so that Test.main () can be called directly.
Because main is the main function of this class, the entry, if each time you enter this portal to go to the instance and then go to invoke the entry method, it is very wordy.

First, static variables

From the bottom, static variables are created and stored in memory once the class is loaded, as long as the class exists, and the static variable exists regardless of whether the object is instantiated or not.

Second, static function

Static functions cannot invoke non-static members, and of course cannot use the This keyword.

Third, static code block

Code that is in a static code block can only be executed once, regardless of how many times the object of the class was created. and static code blocks cannot be executed in non-static functions (especially constructors).

Iv. encapsulation and member access control

Encapsulation is the privatization of a data member in a class (private) so that private members can only be accessed in the class and not be accessed outside the class.

The member control of a class has public, protected, default, Private.

The access control for the class: public, Private.

For classes, the class can only be accessed by all classes of the same package if the class does not have access to the control character before it. If public is added, it can be accessed by all classes in the package outside the package.

For members, the default type is defaulted if no access control is added. The access attributes of a class member are as follows:

1.private: Accessible only within the class

2.default: can be accessed within the class, or it can be accessed by other classes in the package

3.protected: can be accessed within the class, or it can be accessed by other classes in the package, or by subclasses outside the package.

4.public: It can be accessed within the class or by all other classes outside the package.

Four: Package

Build the package reason: directly build files in large projects will be particularly bloated, analogous to the operating system folder, can be well managed.

How to import the package, using the InPort statement.

Java Fundamentals---"Static members, encapsulation, package"------March 6, 2015

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.