简体   繁体   中英

How to pass in the parameter from one class to another in Java?

Forgive me if this is a duplicated question but I'm a beginner in Java and I'm currently trying to get the parameter value from a class called Name and pass that value into another class called Student.

Name Class:

public class Name
{
    public String studentName;


    public Name(String fullName)
    {
        studentName = fullName;
    }
}  

Student Class:

public class Student
{
    private Name studentName;
    private String id;
    private int credits;
    
    public Student(String studentID)
    {
        studentName = new Name("");
        id = studentID;
        credits = 0;
    }
}

What I want to do is to get the parameter value of fullName which is set in the Name class and pass it in studentName = new Name(""); for the Student class instead passing in an empty string to retrieve the name.

What do you mean by "taking the parameter value of fullName , which is set in Name class"? A class will not have a value, you need access to the instance of this class. I'm pretty sure you will have some kind of control class, eg where your main() resides.

At some point you will have created a Name instance:

Name n = new Name("Brandon");

Using this instance of Name class, you can access the actual value:

Student s = new Student("4711", n.studentName);

, assuming you also have included an additional parameter in your Student constructor:

public class Student
{
    private Name studentName;
    private String id;
    private int credits;
    
    public Student(String studentID, String name)
    {
        studentName = new Name(name);
        id = studentID;
        credits = 0;
    }
}

, but this would result in you having 2 different Name objects.

Another option is to pass the object itself as parameter, so both of your objects reference to same object. Changing studentName of either n.studentName and s.studentName would in theoretically result in the value of the respective other being changed as well (I can recall some discussions regarding that topic in Java though).

public class Student
{
    private Name studentName;
    private String id;
    private int credits;
    
    public Student(String studentID, Name nameObject)
    {
        studentName = nameObject;
        id = studentID;
        credits = 0;
    }
}

, which is instantiated by

Name n = new Name("Brandon");
Student s = new Student("4711", n);

You should definitely start reading introductions into object oriented programming, as there are quite a lot of misassumptions just in those few lines of code. The difference between class and object is crucial, also it's usual in those scenarios to have getters/setters rather than public variables in classes. To achieve the kind of dependency you want to have, you might want to look into composition and aggregation in the context of object-orientation. Also the difference between pass-by-value and pass-by-reference is worth looking into.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM