Please explain to me the simple language, the actions in the code.



  • public class Main {
        public static void main(String[] args) {
    
            Cat cat = new Cat();
    
            System.out.println(cat.name);
            changeName(cat);
            System.out.println(cat.name);
        }
    
        public static void changeName(Cat cat) {
            cat.name = "Jerry";
        }
    
        public static class Cat {        
            String name = "Tom";
        }
    }
    


  • starting with method. main, this is the entry point at which the programme begins.

    public static void main(String[] args) {  
    

    Then create a class object Cat by name cat

    Cat cat = new Cat();  
    

    Look at the class.

     public static class Cat {        
            String name = "Tom";
        }    
    

    Class has one characteristic, a string field under the name namein which the value is immediately recorded "Tom"

    We're going to put the field in the console. name Previous class object Cat

    System.out.println(cat.name);     
    

    caused by a static method which, in the parameters, adopts any class object Сat and change the meaning of the field name"Jerry"

     public static void changeName(Cat cat) {
            cat.name = "Jerry";
        }
    

    And at the end, we'll get the new meaning of the field. name facility cat

    System.out.println(cat.name);
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2