M
M
MickeyMouzze2016-08-07 12:15:41
Java
MickeyMouzze, 2016-08-07 12:15:41

Can a method take an object as an argument?

Can a newly created object be used as an argument? Explain how this program works in simple terms

public static void main(String[] args) {
        Student student = new Student();
        printName(student);
        

    }

public static void printName(Student student){
        System.out.println(student.name);
    }

 public static class Student {
        String name = "Amigo";
        int age = 2;
        int money = 0;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NoMoneyException, 2016-08-07
@eugene_leshchinskiy

The algorithm is as follows:
an object of the Student class is created, a
reference to this object is assigned to the student variable, a reference to the object
is passed to the printName function, where its copy is created,
then the copy of the link is used to access the student.name field,
and it is displayed on the console using println
pass you can, you can return. there are no problems. because even the String you're using is a class, just like Student. can it be transferred?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question