Declaration and access control

xiaoxiao2021-03-06  100

Array

In the Java code, the array is an object that is dynamically created. An array can contain several types of variables. These variables may be basic types or object references, and an array can even contain other arrays.

Declare an array variable declares an array variable, code creates a variable to include reference to the array object. It does not create array objects or assign space for array elements. The specified array size is illegal when the declaration is declared. Square brackets can appear as part of the type in the beginning of the declaration, or as part of the array identifier:

Int [] i; // array of int Byte b []; // array of byte object [] o, // array of object short s []; // array of arrays of short

Building an array can build an array with a New operator. Need to include the size of the array and the type of the elements it contain. For multidimensional arrays, you can specify only the first dimension:

Int [] Marks = new int [= "STRING [] [] s = new string [3] [];

The initialization value of the initial array array can be written in a column of the curly brackets by a comma-separated expression:

String s [] = {New String ("apple"), new string ("mango")}; int i [] [] = {{1, 2}, {3, 4}}

You can also initialize arrays by cycles:

INT i [] = new int [5]; for (int J = 0; j

The access array element array index starts from 0, and the N-1 ends, where n is an array size. To get an array size, you need to use an array instance variable called Length. If you try to access an index value outside the range of 0 to N-1, you will throw an ArrayIndexOutOfBoundSexception.

Disclaimer, variables and methods

Now let's take a look at the method for modifying, methods and variables. There are two modifiers - access modifiers and non-access modifiers. Access modifiers allow us to access or provide more access to code restrictions.

Access modifiers available for class modes include public, private, and protected. However, a top class can only have public and default access levels. This class will have default access if you do not specify an access modifier. Only classes in the same package can see classes with default access. If a class is declared as public, then the classes in other packages can also access it.

Let's take a look at some of the effects of non-access modifiers. Final Keywords (For more content on keywords, please refer to Java Keywords and Identifiers) Do not allow classes to expand. The Abstract class cannot be instantiated, but it can be extended by subclass:

Public Final Class Apple {..} Class GreenApple Extends Apple {} // NOT ALLOWED, Compile Time Error

Method Memory and variable modifiers All access modifiers can be used for members of the class. Private members can only be accessed within the class. The protected members can only be accessed by the class or subclass of such a package. Public members can be accessed by all other classes.

If you do not specify an access modifier, these members will have default access, only other classes in the same package can access them.

Now let's explore other modifications that can be used for members' statements. Some of them can only be used for methods, and others can only be used for variables, as shown below:

Figure 1. Method of modifiers for methods and variables

Synchronized methods can only be accessed by one thread at a moment. The Transient variable cannot be serialized. The Abstract method does not have an implementation, it must be implemented by the first specific subclass containing the class. Classes that at least contain an Abstract method must declare to Abstract. However, the Abstract class does not necessarily need to include Abstract method: public abstract class myabstractclass {public abstract void test ();

Native modifiers indicate that this method is not written in a native language with a Java language. StricTFP Keywords (more content about keywords, please refer to Java Keywords and Identifiers) Only for methods and classes, it enforces floating point to comply with the IEE754 standard. Variables can be declared as Volatile, in which case the thread must coordinate the work replica of the field at each access to this variable and the main copy.

The Static variable is shared by all instances of the class. STATIC methods and variables can be used without any instances at all.

Class StaticTest {Static Int i = 0; static void getvar () {i ; system.out.println (i);}} class test {public static void main (string args []) {statictest.getvar ();}}

The constructor uses the constructor when you create an object with a class. The constructor name must match the class name and must have no return type. They can be overloaded, but they are not inherited by subclasses. The call constructor can only call constructor from other constructor. To call the constructor in the same class, call the this () function with the matching parameter. To call the constructor in the superclass, call the super () function with the matching parameter. When creating a subclass object, all superclatrios constructor is called from top to the next order in the hierarchy. The default constructor does not provide any other constructor in the class, the compiler will create the default constructor. It doesn't have any parameters. The default constructor calls the superclassless parameter constructor. It has the same access modifier. However, even if a constructor is written in the class, the compiler will not provide the default constructor. For example, the following class has a constructor that defines two parameters. Here, if we try not to pass the parameters, instantiate this class, then the compiler will give an error because there is no default constructor: Class Dot {Int x, y; DOT (int x, int y) {this.x = x; THIS.Y = Y;}}

If you call the default constructor of the class, the superclass does not have a constructor without parameters, then your code will not be compiled. The reason is that the default constructor of the subclass implicitly modulates its superclassless parameter constructor. For example: Class Dot {Int x, Y; DOT (INT X, INT Y) {this.x = x; this.y = y;}} class mydot extends Dot {} class test {public static void main (string args ]) {MYDOT DOT = New mydot ();}} Summary This section we discussed important concepts in the first goal. We discussed an effective way to declare and build one dimensional and multi-dimensional array. Be sure to grasp the legal combination of the modifier when you understand the effects of the method and the modifier of the class. For example, a Final method cannot be declared as Abstract. We also learned about the contents of constructor. Remember, the compiler only provides the default parameterless constructor exercise without writing any constructor.

problem:

What is the result of compiling and run the following?

Class Box {Int B, W; Void Box (INT B, INT W) {this.b = b; this.w = W;}} public class mybox extends box {mybox () {super (10, 15); system .out.println (b "," w); static public void main (string args []) {mybox box = new mybox ();}}

Option:

A. Cannot compile, the main method does not declare the correct declaration B. Output 10, 15 C. Output 0,0 D. The above is not the correct answer:

D illustrate:

This program cannot be compiled because there is no matching constructor in the base class for the SUPER (10, 15) call in the subclass constructor. Void Box (int B, int W) is not a constructor because it gives the return type. If it is a constructor, the variables W and h will be initialized to 10 and 15. This program can be compiled and output 10, 15. There is no error in the main () method, static and public and public can appear in any order.

转载请注明原文地址:https://www.9cbs.com/read-123517.html

New Post(0)