Methods of a class

No arguments method

Grammar:

publicReturn value type Method name (argument list){// The method body
}
Copy the code

Knock: method with no return value

public void sayHi(a){
    System.out.println("Hello");
}
Copy the code

No return value The return value is void. In the preceding example, there is no return value. If a method has a return value, the method must use the key return to return the value of the type of the returned value.

Knock: there are return value methods

public class Student {
	String name;
	int age;
	/** * get age *@return* /
	public int getAge(a) {
		return age;
	}
	/** * get the name *@return* /
	public String getName(a) {
		returnname; }}Copy the code

Knock: Calls a method with a return value

public class Main {
	public static void main(String[] args) {
		Student stu=new Student();
		stu.age=10;
		stu.name="Zhang";
		// Call method 1 with arguments to receive the return value directly
		int age=stu.getAge();
		System.out.println("Age:"+age);
		// Call method 2 with arguments that return the method as an element
		String hello="Hello, I am:"+stu.getName(); System.out.println(hello); System.out.println(stu.getName()); }}Copy the code

Method do.

Define the syntax for parameterized methods:

< access modifier > Return type < method name >(< formal argument list >){method body}Copy the code

Multiple parameters are allowed. The parameters used when declaring are called parameters, and the values actually passed in when calling are called arguments. Call a method whose arguments correspond to the argument list.

Knock: Definition and invocation of parameterized methods

public class DemoMethod {
	public void sayFriend(String name1,int num) {
		System.out.println(name1+"A"+num+"A good friend!);
	}
	public static void main(String[] args) {
		DemoMethod method=new DemoMethod();
		String name="Zhang";/ / arguments
		int num=2;
		method.sayFriend(name, num);// Pay attention to the order}}Copy the code

Arrays and objects can also be tapped as arguments:

public class DemoParam {
	/** * Simulates the arrays.tostring () method to print Arrays *@param names
	 */
	public void showArray(String[] names) {
		System.out.print("[");
		for (int i = 0; i < names.length; i++) {
			System.out.print(names[i]);
			if (i<names.length-1) {
				System.out.print(",");
			}
		}
		System.out.println("]");
		System.out.println();
	}
	public static void main(String[] args) {
		String[] names= {"Zhang"."Bill"};
		DemoParam demo=newDemoParam(); demo.showArray(names); }}Copy the code

Member variables and local variables

Depending on where a variable is declared, the scope is also different, which determines where a variable can be accessed. Member variables are declared directly in a class and local variables are declared in a method.

Tap: Feel local and member variables

public class Student {
	String name="Bill";// Member variables
	int age=29;// Member variables
	/** * get age *@return* /
	public int getAge(a) {
		int age=10;// Local variables
		return age;
	}
	/** * get the name *@return* /
	public String getName(a) {
		int age=20;// Local variables
		return name;
	}
	public static void main(String[] args) {
		Student stu=new Student();
		System.out.println("Age:"+stu.getAge());/ / 10
		System.out.println("Name:"+stu.getName()); }}Copy the code

Member variables can be accessed in all instance method local variables can only be used in the current method of statement, multiple methods can declare local variables are local variables of the same name and member variable names at the same time, the current method is preferred to use local variables in Java can give a member variable an initial value, not for local variable initial value

package

There are a number of classes in the JDK, including some with the same name. How does the JDK distinguish between these classes? Using packages solves this problem.

Grammar:

packageThe package name./ / declare package
importThe package name./ / import packages
public classThe name of the class{member variable methods... }Copy the code

Declare the name of a class using the package keyword. Package must be the first line of all statements, and only one. Use the import keyword for classes in a package. You can write more than one, below the package and above the declared class. You can import packages using fully qualified names, such as import java.util.date; , or all classes in the package such as import java.util.*;

Package and directory relationship: creating the package com.jikedaquan is equivalent to creating the directory structure com\jikedaquan

Package name specification: The package name consists of lowercase letters and cannot start or end with a dot. The package name is best preceded by a unique prefix, usually using an organization inverted network domain name such as www.jikedaquan.com. The package name com.jikedaquan varies according to the internal specifications of different organizations.

Also search for creating packages in Eclipse, not covered in detail in this article.

Search the concerned public number “enjoy wisdom peer”, the first time to obtain technical dry goods