Java Learning Notes

Source: Internet
Author: User

Why can a Java file contain only one public class?

Java programs are executed from the main function of a public class (actually, the main thread), just as the C program starts with the main () function. Only one public class is available to facilitate the class loader. A public class can only be defined in a file with its class name as its file name.

Java requires a uniform class name (the only public class) and a file name because it is mandatory, so you do not need to explicitly declare it when referencing other classes. At compile time, the compiler looks for files of the same name based on the class name.

Java Import package (different from Python)

Import  java.io.*;

The role of the package is the namespace of C + + to prevent collisions between classes with the same name. At compile time, the Java compiler directly generates the generated class file to the corresponding directory directly based on the information specified by the package. For example, the package AAA.BBB.CCC compiler generates the various classes under the. java file into the ./aaa/bbb/ccc/directory.

Import is to simplify the instantiation of code after the use of the package.  Suppose ./aaa/bbb/ccc/under Class A, if there is no import, instantiate Class A as:new AAA.BBB.CCC.A (), using the import AAA.BBB.CCC.A, you can directly use new A () , that is, the compiler matches and expands the AAA.BBB.CCC. String.

The difference between a member variable and a class variable

A variable modified by static is called a static variable, which is essentially a global variable. If a content is shared by all objects, the content should be statically decorated, and the content that is not statically decorated is actually a special description of the object.

Instance variables of different objects are assigned different memory spaces, and if the member variables in the class have class variables, then all of the object's class variables are assigned to the same memory, and changing the class variable of one object affects this class variable of other objects, that is, the object shares the class variable.

The difference between a member variable and a class variable:

1, two variables have different life cycles

Member variables exist as objects are created and released as objects are reclaimed.

Static variables exist as the class loads, disappearing as the class disappears.

2. Different calling methods

Member variables can only be called by an object.

A static variable can be called by an object and can also be called by a class name.

3. Different aliases

Member variables are also known as instance variables.

A static variable is also known as a class variable.

4. Different Data storage locations

Member variables are stored in the object of the heap memory, so they are also called object-specific data.

Static variable data is stored in the static area of the method area (shared data area), so it is also called the shared data of the object.

The static keyword, which is a modifier used to decorate members (member variables and member functions).

Characteristics:

1. Object sharing that wants to implement the common data in the object. This data can be modified statically.

2. Members that are statically decorated can be called directly by the class name. In other words, static members are more of a calling method. The class name. Static mode.

3, static load with the load of the class. and takes precedence over the existence of the object.

Disadvantages:

1, some data is an object-specific data, it can not be static modification. Because of that, the unique data becomes the shared data of the object. There is a problem with the description of the thing. Therefore, when defining static, it is important to be clear whether the data is shared by the object.

2. Static methods can only access static members and cannot access non-static members. Because the static method loads, it takes precedence over the existence of the object, so there is no way to access the members of the object.

3. This,super keywords cannot be used in static methods. This cannot be used because this represents an object, while static is possible without an object.

When do you define static members? Or, when defining a member, does it need to be statically decorated?

There are two types of members:

1, member variables. (data sharing is static)

Whether the data for this member variable is the same for all objects:

If so, then the variable needs to be statically decorated because it is a shared data.

If not, then say that this is the object's unique data to be stored in the object.

  2, member functions. (defined as static when no unique data is called in the method)

What if the member function needs to be statically decorated?

For reference, the function has access to the unique data within the object:

If there is access to unique data, the method cannot be statically decorated.

If no unique data has been accessed, then this method needs to be statically modified.

Java Learning Notes

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.