[Java Basics] several simple call relationships and methods, and several java-based calls

Source: Internet
Author: User

[Java Basics] several simple call relationships and methods, and several java-based calls

Run the Code directly.

1 class lesson4AB 2 // public modifier method A under the same class, B can call each other 3 {4 public void A () 5 {6 B (); // equivalent to this. B (); 7} 8 public void B () 9 {10 A (); // equivalent to this. A (); 11} 12} 13 class lesson4C14 // The public modifier method that exists in different classes C call B15 {16 public void C () 17 {18 lesson4AB lAB = new lesson4AB (); 19 lAB. B (); 20 // the upper and lower methods are equivalent. You can call 21 new lesson4AB (). B (); 22} 23} 24 class lesson4DE25 // The static and non-static methods in the same class call each other. 26 {27 public static void D () // modified with static, belongs to class member 28 {29 // E (); // error: the non-static method E () 30 cannot be referenced from the static context // [static is not referenced from the Static Context] 31 32 // this. E (); // error: the non-static variable this33 cannot be referenced from the static context. // [static and this cannot coexist] 34 35 new lesson4DE (). E (); // do not use static modifier through 36 // public void E (). It belongs to an object and can only be used to call 37} 38 public void E () // No static modification is used. It belongs to object 39 {40 D (); // equivalent to this. D (); 41 // the object can call class member 42} 43} 44 class lesson4 45 {46 public static void main (String [] args) 4 7 {48 System. out. println ("May you be happy! "); 49} 50}

 

For a detailed explanation of the above code line 35th:

Class methods cannot call non-class methods, so it is wrong to call E () directly. But it can be called through an object, so a new object is required to call it (non-static method ).

The following is a good summary on the Internet:

A class is an abstract concept, and an object is an entity. The static method can only allocate one space in the memory. No matter how many times the reference is directed to that space, that is, resident memory, it is a class object, not a single instance. Non-static methods are generated with objects. Every time an object is generated, a non-static method is generated and different spaces are allocated. Therefore, non-static members must depend on objects. (2014.10.31 modify and add)

 

 

I am a newbie, and the above is a summary of my online self-learning experience. If you have any mistakes, please note. There are a lot of new kids shoes to talk about, and great gods give more advice. I wish you a happy life.

 


Write a Java program to implement simple method calls

What method is called?
Class method? Constructor? Does it contain parameters?
No. You still want code !!!!! Wait !!!!!
Modify: give points, oh...
All right, write it... you just learned it. Let me write it like this.
The so-called class method consists of four parts:
1. Method Name
2. Data Type of the method Return Value
3. Parameter List
4. method subject
Syntax:
<Returntype> <methodname> (<type1> <arg1>, <type2> <arg2> ....){
<Set of statements>
}
Returntype is the return value type.
<Methodname> is the name of a custom method.
The method parameter list is the name of a group of variables separated by commas (,).
The statement \ expression \ and Method \ can be stored in the body of the method. If the method has a return value type, the return keyword must be used. void is not returned.
Write a code snippet for you ..
Class Book {
String bookName;
String authorName;
Int nopages;
Boolean available;
// The above is casual
Void isAvailable (){
If (available = true)
{
System. out. println ("YES ");
}
}
}
The following describes how to implement the Book object in the main method.
Example: objBook
ObjBook. isAvailable ();
That's all.
Give me more points. Typing is not easy.

Describes how to call a method in java.

1. Non-access modifier.

1. abstract class:
Classes modified with abstract modifiers are called abstract classes. Abstract classes are conceptual classes without specific objects.
Abstract classes are an optimized way to organize concepts: Abstract Common Features. When describing and processing a specific object, you only need to describe different special features. This organizational structure makes all concepts distinct and concise, and conforms to people's daily thinking habits.
Abstract classes are a set of public attributes of all their subclasses. One advantage of using abstract classes is that they can be fully utilized to improve the efficiency of program development and maintenance.
It is worth mentioning that the object-oriented technology is to deal with practical problems in a way closer to the human way of thinking. The establishment of abstract classes is one of the concrete embodiment of this idea, it is the product of imitating human thinking patterns.

2. Final class:
If a class is modified and qualified by the final modifier, it indicates that this class cannot have child classes.
A class defined as final is usually a class that has a fixed effect and is used to complete certain standard functions. For example, the InterAddress and Socket classes defined by the Java system to implement network functions are all final classes.
Abstract and final modifiers cannot modify a class at the same time, because abstract class itself does not have a specific object, you need to derive a subclass before creating the subclass object; and final class cannot have a subclass. It makes no sense to put them together.

3. Some class modifiers can also be used to modify the fields or methods in the class:

(1) domain: it is the static attribute of the class and object. The operation of the defined domain is the operation of variable or object creation.

<1> static domain:
The domain modified with the static modifier only belongs to the static domain of the class. Static domains are the domains shared by each object in the class. They are class domains and do not belong to any specific object of a class. It is a public storage unit. When any Class Object accesses it, it obtains the same value.

<2> static initializes:
The static initiator is a statement group enclosed by braces guided by the keyword static. The role is to initialize the static domain of the class during loading.
Like constructors, they are used to complete initialization, but the static initiators are three different from constructors:
① Constructor initializes each newly created object, while the static initiator initializes the class itself.
② Constructor is automatically executed by the system when a new object is generated using the new operator, while the static initiator is called and executed by the system when the class to which it belongs is loaded to the memory.
③ Unlike the constructor, the static initiator is not a method and has no method name, return value, or parameter list.

<3> final domain:
The domain modified with final is actually a constant in Java.
Note the following when using the final modifier to describe constants:
① It is necessary to describe the data type of the constant.
② You must also specify the specific values of constants.
③ The values of constant members of all class objects are fixed and consistent. To save space, constants are usually declared as static.

<4> easy loss of domain:
If a domain is modified by the volatile modifier, it indicates that this domain may be controlled and modified by several threads at the same time, that is, this domain is not only controlled by the current program, during running, other unknown program operations may affect and change the value of this field. Pay special attention to this feature.
Generally, volatile is used to modify the fields that accept external input. For example, the variable indicating the current time can be modified at any time by the background thread of the system to ensure that the latest system time is obtained in the program. Therefore, it can be defined as a loss-prone domain.

(2) method: it is a dynamic attribute of the class, marking the functions and operations of the class. Parentheses are the identifier of the method.

<1> abstract method:
The modifier abstract modifier's abstract method is a method with only the method header, without specific methods and operations... the rest of the full text>

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.