Use object Step 1, create object: Class Name Object name = new class name (), (Instantiate a Class) example: Telphone phone =new telphone (); 2, using object: Reference the object's Property object name. Properties Example: Phone.screen = 5;//Assigning a Reference object method to an Instance object : Object name. Method name (); Example: Phone.sendmessage ();
member variables and local variables:
Called member variables that are written directly in the class for use by the entire class. Written in the method is called local variable 1. A local variable scope differs from a member variable to an entire class, and a local variable acts on the method 2 that defines it. The member variable will have an initial value of NULL or 0. Local variables need to be assigned a value of 3. Local variables with the same name are allowed in different methods and local variables of the same name are not allowed in the same method 4. Local Variable with the same name, local variable precedence is more
Construction Method:
1. The construction method is a method for initializing an object 2. Using new+ Constructing a method to create an object 3. The constructor method can have parameters, but there is no return value type, the method name is the same as the class name 4. The constructor method with parameters is used to assign a value of 5 when initializing. The system comes with an parameterless construction method, and its own definition of construction method is equivalent to overriding the construction method 6. Method overloading
7. No parameter construction method is automatically added when there is a specified construction method, either with or without a parameter.
1 // 2Main ():3new telphone (); 4 class Telphone: 5 Public Telphone () {6 System.out.println ("Telphone no-parameter construction method executed!") ); 7 }
1 // Constructed method with arguments 2 telphone phone2 = new Telphone (4.0f,2.0f,2.0f 3 class Telphone: 4 public telphone (float newscreen,float newcpu,float Newmem) { 5 screen =< Span style= "COLOR: #000000" > Newscreen; 6 CPU = newcpu; 7 mem = Newmem; 8 }
Static variables used in Java
Members in Java that are decorated statically are called static members or class members. It is owned by the entire class, not by an object, which is shared by all objects of the class. Static members can be accessed directly by using the class name or by using the object name. Of course, given the particularity of his role, it is more recommended to access the class name ~ ~
Use static to modify variables, methods, and blocks of code.
In this section, let's take a look at static variables first.
For example, we define a static variable hobby in the class, and the operation code is as follows:
Operation Result:
Note: Static members belong to the entire class, and when the system first uses the class, it allocates memory space until the class is unloaded for resource recycling! ~~
Static methods for using static in Java
As with static variables, we can also use the static modification method, called a static method or a class method. In fact, the main method we've been writing about is static methods. The use of static methods such as:
Operation Result:
Need to note:
1 static members can be called directly in a static method, but not non-static members directly . Such as:
if you want to invoke a non-static variable in a static method, you can access the non-static variable by creating the object of the class and then through the object. such as:
2, in the ordinary member method, you can directly access the same kind of non-static variables and static variables, as follows:
3, static methods can not directly invoke non-static methods, you need to access the non-static method through the object . Such as:
Anyway:
Static member variables or static methods can be used directly in static methods; for non-static variables and methods you need to create objects to use;
The same non-static variables and static variables can be accessed directly in the normal method.
Static initialization block used in Java
In Java, data can be assigned by initializing blocks. Such as:
In the declaration of a class, you can include multiple initialization blocks, which are executed sequentially when you create an instance of the class. If you initialize a block with the static adornment, it is called a static initialization block.
It is important to note that static initialization blocks are only executed when the class is loaded and only once, while static initialization blocks can only assign values to static variables and cannot initialize normal member variables.
Let's look at a piece of code:
Operation Result:
by outputting The result, we can see that the static initialization block is executed first when the program is run, then the normal initialization block is executed, and the construction method is finally executed. Because static initialization blocks are only executed once when the class is loaded, static initialization blocks are not executed when the object Hello2 is created again.
java-classes and objects