Method call recursion
- Static method
- Non-static methods non-static method can call static methods , static can not call non-static .
-
public static void main (String []args){ //静态方法 static //非静态方法 //给学生类加个static 加上类名 可以直接调用过来 Student.say(); //实例化这个类 //对象类型 对象名字 = 对象的值 Student student = new Student(); student.open(); } /* public class Studen{ //方法 public static void say (){ System.out.println("下课了,学生开始说话"); } public void open(){ System.out.println("门打开了"); } } */ //但是我们一般调用都不加static,所以需要new //new student
public static void main (String []args){
}
//静态static 和类时就一起加载
//类实例化 之后才存在
public void a (){
b(); //可以直接调用b,两个都为static的时候也可以相互调用
}
public void b (){
}
- Participation
public static void main (String []args){
//方法调用
// 非静态方法调用
Demo04 demo04 = new Demo04();
//(2,4)代表实际参数。
//实际参数与形式参数的内型要一样。
int add =demo04.add(2,4);
System.out.println(add);
}
public int add(int a,int b){
//(int a,int b)代表形式参数
return a+b;
}
- Pass by value and pass by reference
Value transfer: The actual parameter and the formal parameter are two independent variables in the memory.
Pass by reference: The actual parameter and the formal parameter point to the same address in memory.
public class Demo02 {
public static void main(String[] args) {
//值传递
int a = 1;
System.out.println(a); //1
System.out.println("==========");
//调用
Demo02.change(a);
System.out.println(a);//1
}
//值传递:实参和形参在内存上是独立的两个变量。所有
// 引用传递:实参和形参在内存上指向的是同一个地址。
//返回值为空
public static void change (int a){
a = 10;
}
}
public class Demo01 {
//引用传递 :对象 本质还是值传递
//对象 内存!
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name); //null
System.out.println("=============");
//调用change
change(person);
System.out.println(person.name);//jack
}
public static void change(Person person){
//Person 是个对象指向的 Person person = new Person();
//这是一个具体的人可以改变
person.name = "jack";
}
//定义一个Person类,有个属性:name
static class Person{
String name;
}
}
- this keyword