Method calls and parameter passing

Static methods: There modified static method.

Non-static methods: no static modification method.

  1. Method call:

A static method call static methods / properties

1) a class: direct call. 

2) different types / different files:

a:. class name attribute name / method name

b: instantiate the object. Object class name name = new class name ();

                                                          Object Name Property / Method

           Two static call non-static methods / properties

We are the first instance of the object. Object class name name = new class name ();

                                                                 Object name attribute name / method name

           A non-static static method call

           Two non-static non-static method call

            1) in the same class: direct call

            2) different classes:

                                a:. class name method (only static properties)

b: Examples of object

Summary: three cases can be called directly

1. Static transfer a static class.

2. Central African static call a class static / non-static.

3. Static class name. Static properties / static methods.

public  class Demo03 {
     int Age;
     public  static  void main (String [] args) { 
        System.out.println (Demo04.name); // called statically static. 1 
        Demo04.eat (); 

        Demo04 D = new new Demo04 (); / / static static call 2 
        System.out.println (d.name); 
        d.eat (); 

        Demo03 D1 = new new Demo03 (); // static nonstatic 
        d1.method (); 
        System.out.println (D1. Age); 
    } 
    public  void Method () { 
        System.out.println ("first method");
    }

}
public  class Demo04 {
     static String name = "John Doe" ; 

    public  static  void EAT () { 
        System.out.println ( "Hamburger" ); 
    } 
}
public  class Demo05 {
     static  int Age; 
    String name; 
    public  static  void main (String [] args) { 

        Demo05 D1 = new new Demo05 (); // Static transfer of non-static instance 
        d1.method (); 
    } 

    public  void Method () { 
        System.out.println (Age); // nonstatic modulation static     
        the method1 ();                  // nonstatic modulation static     
        System.out.println (name); // nonstatic modulation nonstatic     
        method2 ();                  // nonstatic adjusted non-static    
        System.out.println("first method");
    }
    public static void method1(){
        System.out.println("second method");
    }
    public void method2(){
            System.out.println("third method");
    }
}
public  class Demo06 { 

    public  static  void main (String [] args) { 

        Demo06 D1 = new new Demo06 ();    // Static transfer of non-static instance 
        d1.method (); 
    } 
    public  void Method () { 
        System.out.println ( person.name);    // nonstatic modulation static 
        Person.method1 ();                   // nonstatic modulation static 
        the Person P = new new the Person ();            // nonstatic modulation of non-static instance 
        p.method2 (); 
        the System.out. the println ( "First Method" ); 
    } 
} 
class Person{
    static String name;
    int age;
    public static void method1(){
        System.out.println("second method");
    }
    public void method2(){
            System.out.println("third method");
    }
}
public  class Demo09 {
     // argument to parameter is unidirectional, the parameter value changes will not affect the actual parameter during delivery 
    public  static  void main (String [] args) {
         int I =. 1 ; 
        String S = "WW" ; 
        Demo09 D = new new Demo09 (); 
        d.method (I, S); 
        System.out.println (I); 
        System.out.println (S); 
    } 
    public  void Method ( int I, S String) { 
        I = 100 ; 
        S = "ASD" ; 
    } 
        public  void the method1
}
public class ChuanDiZhi{
    int x = 3;
    public static void main(String args[]){
        ChuanDiZhi p = new ChuanDiZhi();
        p.x = 9;
        show(p);
        System.out.println(p.x);
    }
    public static void show(ChuanDiZhi p){
        p.x = 4;
    }        
}

Whether the return value

There void modification, no return value

int ------------------> return int type value

string --------------> return String type value

Data type --------> return current data type value

Returns the class, set, flow, etc.

The method return type and must return the value of the same data type after

 

The method of passing parameters

Parameter: the parameter form, when defining the parameters of the method are carried.

Arguments: the actual parameters, method calls the argument passed.

Parameter arguments and the difference between:

         Only 1 parameter is invoked, it allocates memory unit, the immediate release of the memory unit at the end of the call

                   Parameter is valid only within the current methods

2 arguments can be constants, variables, expressions, and other methods. But before making the method call, the value must be determined.

3 and parameter arguments in sequence, type, length, must be the same.

4 parameter arguments to be unidirectional,

Parameters by value

A transfer value: When you run the function, parameters, and arguments in a different memory location, a copy of the parameter values ​​of the argument. At the end of the function operation parameter is released, the value of the argument does not change.

Address transfer 2: When you run the function, the parameter is passed the address of the argument, then the parameter is modified when the actual parameters are also changed.

 

Non-static methods: no static modification method.

  1. Method call:

A static method call static methods / properties

1) a class: direct call. 

2) different types / different files:

a:. class name attribute name / method name

b: instantiate the object. Object class name name = new class name ();

                                                          Object Name Property / Method

           Two static call non-static methods / properties

We are the first instance of the object. Object class name name = new class name ();

                                                                 Object name attribute name / method name

           A non-static static method call

           Two non-static non-static method call

            1) in the same class: direct call

            2) different classes:

                                a:. class name method (only static properties)

b: Examples of object

Summary: three cases can be called directly

1. Static transfer a static class.

2. Central African static call a class static / non-static.

3. Static class name. Static properties / static methods.

public  class Demo03 {
     int Age;
     public  static  void main (String [] args) { 
        System.out.println (Demo04.name); // called statically static. 1 
        Demo04.eat (); 

        Demo04 D = new new Demo04 (); / / static static call 2 
        System.out.println (d.name); 
        d.eat (); 

        Demo03 D1 = new new Demo03 (); // static nonstatic 
        d1.method (); 
        System.out.println (D1. Age); 
    } 
    public  void Method () { 
        System.out.println ("first method");
    }

}
public  class Demo04 {
     static String name = "John Doe" ; 

    public  static  void EAT () { 
        System.out.println ( "Hamburger" ); 
    } 
}
public  class Demo05 {
     static  int Age; 
    String name; 
    public  static  void main (String [] args) { 

        Demo05 D1 = new new Demo05 (); // Static transfer of non-static instance 
        d1.method (); 
    } 

    public  void Method () { 
        System.out.println (Age); // nonstatic modulation static     
        the method1 ();                  // nonstatic modulation static     
        System.out.println (name); // nonstatic modulation nonstatic     
        method2 ();                  // nonstatic adjusted non-static    
        System.out.println("first method");
    }
    public static void method1(){
        System.out.println("second method");
    }
    public void method2(){
            System.out.println("third method");
    }
}
public  class Demo06 { 

    public  static  void main (String [] args) { 

        Demo06 D1 = new new Demo06 ();    // Static transfer of non-static instance 
        d1.method (); 
    } 
    public  void Method () { 
        System.out.println ( person.name);    // nonstatic modulation static 
        Person.method1 ();                   // nonstatic modulation static 
        the Person P = new new the Person ();            // nonstatic modulation of non-static instance 
        p.method2 (); 
        the System.out. the println ( "First Method" ); 
    } 
} 
class Person{
    static String name;
    int age;
    public static void method1(){
        System.out.println("second method");
    }
    public void method2(){
            System.out.println("third method");
    }
}
public  class Demo09 {
     // argument to parameter is unidirectional, the parameter value changes will not affect the actual parameter during delivery 
    public  static  void main (String [] args) {
         int I =. 1 ; 
        String S = "WW" ; 
        Demo09 D = new new Demo09 (); 
        d.method (I, S); 
        System.out.println (I); 
        System.out.println (S); 
    } 
    public  void Method ( int I, S String) { 
        I = 100 ; 
        S = "ASD" ; 
    } 
        public  void the method1
}
public class ChuanDiZhi{
    int x = 3;
    public static void main(String args[]){
        ChuanDiZhi p = new ChuanDiZhi();
        p.x = 9;
        show(p);
        System.out.println(p.x);
    }
    public static void show(ChuanDiZhi p){
        p.x = 4;
    }        
}

Whether the return value

There void modification, no return value

int ------------------> return int type value

string --------------> return String type value

Data type --------> return current data type value

Returns the class, set, flow, etc.

The method return type and must return the value of the same data type after

 

The method of passing parameters

Parameter: the parameter form, when defining the parameters of the method are carried.

Arguments: the actual parameters, method calls the argument passed.

Parameter arguments and the difference between:

         Only 1 parameter is invoked, it allocates memory unit, the immediate release of the memory unit at the end of the call

                   Parameter is valid only within the current methods

2 arguments can be constants, variables, expressions, and other methods. But before making the method call, the value must be determined.

3 and parameter arguments in sequence, type, length, must be the same.

4 parameter arguments to be unidirectional,

Parameters by value

A transfer value: When you run the function, parameters, and arguments in a different memory location, a copy of the parameter values ​​of the argument. At the end of the function operation parameter is released, the value of the argument does not change.

Address transfer 2: When you run the function, the parameter is passed the address of the argument, then the parameter is modified when the actual parameters are also changed.

 

Guess you like

Origin www.cnblogs.com/yueluoshuxiang/p/11367091.html