Non-parametric methods for Java classes

Source: Internet
Author: User

Java Annotations:
: Single-line comment
/**/: Multiline Comment
/** */:javadoc Notes
Method:
Grammar:


Access modifier return value type method name () {
Method body
}

Example:
public void Run () {
System.out.println ("");
}
Public String Robball () {
String ball= "Ball";
return ball;
}
The return value of the method:

1. If the method has a return value, the method must return the value using the keyword return, the type of the return value type
return expression, function: Jump out of method, return result
2. If the method does not return a value, the return value type is void

Case:
public class Lion {
Color Properties
String color;

int age;

public void Run () {

System.out.println ("Lion is Running");
}

public void Say () {
System.out.println ("The lion is hungry and calls to eat");
}

Public String Robball () {
System.out.println ("hehe");
String ball= "Volleyball";

return ball;
}

Getting the lion's age
public int getage () {
int age=18;
return age;
}
}
public class Test {
public static void Main (string[] args) {
Lion lion = new lion ();

/*lion.run ();

String ball = Lion.robball ();


System.out.println ("Lion is playing:" + ball);
System.out.println ("Come back after the method has been executed!") ");
*/

System.out.println (Lion.getage ());

}
}
Local variables and member variables:
Different scopes
The scope of a local variable is limited to the method that defines it
The scope of a member variable is visible within the entire class
Different initial values
Java will give the member variable an initial value
Java does not give initial values to local variables
Attention:
Local variables with the same name are not allowed in the same method
In different methods, you can have local variables of the same name
Local variables have higher precedence in the same class when the member variable and local variable have the same name


Non-parametric methods for Java classes

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.