Search code examples
javavariablesparametersconstructorrelationships

Not understanding relationship between variables between classes


I'm new here so go easy. I tried the search function but couldn't find much (the closest responses were in python and C++). I have a fragile understanding of passing "variables" (or is it parameters?) from class to class and wanted to post here to see if anyone can help shed light on this. I am in an online class and I've posted my code below. We were asked to take an input for a course name and instructor name and output it, in addition to another a course and instructor name that is static (does not take any input). I'm having trouble understanding the relationship between courseName, name (which is in both GradeBook and GradeBookTest), and setCourseName. I've tried re-reading sections in my book (which is not the best), watching youtube videos, etc but I'm just not following here. I feel like I am oh so close to understanding it, too!

So to start off, Does the "String name" in GradeBookTest have any relationship with the "String name" in the constructor of GradeBook? Are they the same "objects"?

Also, why does "String name" have to be in parentheses in the setCourseName method? It is already initialized in the constructor, and so the method setCourseName should be able to pull in String name without it there, right?

So it seems like the order of operations are:

  1. Class GradeBookTest runs and initializes "String name".
  2. It assigns what the user inputs from the first question into name. name is now a placeholder for whatever the user input was.
  3. What happens with name now that is in GradeBook and in the parentheses of the constructor?
  4. It then seems that the "object" setCourseName is being set as whatever is in name from the GradeBookTest class (user input)
  5. I'm just not sure what is happening from the constructor through the next method (setCourseName), in terms of the String name being passed around. Can someone explain what is happening here, please?

    public class GradeBook
    {
       private String courseName; // course name for this GradeBook
       private String instructorName; // STEP A - Name of the instructor for the course
    
       // constructor initializes courseName with String argument
       public GradeBook( String name, String teacher ) { // constructor name is class name
         setCourseName(name); // initializes courseName
          setInstructorName(teacher); // STEP C - initializes instructorName
      } // end constructor
    
    
       // method to set the course name
       public void setCourseName( String name ) {
          courseName = name; // store the course name
       } // end method setCourseName
    
    
       // method to retrieve the course name
       public String getCourseName() {
          return courseName;
       } // end method getCourseName
    
    
       // display a welcome message to the GradeBook user
       public void displayMessage() {
          // this statement calls getCourseName to get the 
          // name of the course this GradeBook represents
          System.out.printf( "Welcome to the grade book for\n%s!\n", 
             getCourseName() );
          System.out.printf( "This course is presented by:\n%s!\n",
             getInstructorName() );
       } // end method displayMessage
    
    
       // method to set instructor name
       public void setInstructorName( String teacher) {
    
         //prompt for and read instructor name
           instructorName = teacher; //stores the instructor name
       } //end method setInstructorName
    
       //method to retreive instructor name
       public String getInstructorName() { 
           return instructorName;
       } // end method getCourseName
    
    } // end class GradeBook
    

AND NOW FOR GRADEBOOKTEST

import java.util.Scanner;

public class GradeBookTest
{
   // main method begins program execution
   public static void main( String[] args )
   {

      String teacher;
      String name;

      Scanner input = new Scanner(System.in); // initialize scanner for input from user
      System.out.printf("Please enter the course name: ");
      name = input.nextLine();
      System.out.printf("Please enter the instructor's name: "); // prompt for input
      teacher = input.nextLine(); // obtain user input of the instructor name  

      // create GradeBook object
      GradeBook gradeBook1 = new GradeBook(name, teacher);
      GradeBook gradeBook2 = new GradeBook(
         "CIS 254 - Intro to object-oriented programming", "Professor Green" );

      gradeBook1.displayMessage();
      System.out.println() ;
      gradeBook2.displayMessage();
      System.out.println();
      System.out.printf("Programmed by Grant Ognibene!\n");

   } // end main
} // end class GradeBookTest

Thanks!


Solution

  • first thing executed is main method in your GradeBookTest class. there you have written

    String name; name = input.nextLine();

    so name will get value and it will store it in constant pool.

    while creating object you are passing the value to name to constructor in line

    GradeBook gradeBook1 = new GradeBook(name, teacher);

    name's value will be assign to local variable (name) of constructor of GradeBook. inside constructor you are again calling setter method setCourseName.

    String name is local variable in setter method.so value will be assigned to name variable and then you are assigning it to instance variable courseName.

    ultimately only single entry will we created in constant pool GradeBook class. variable will be created multiple times because those are local variable but string literal will be inserted in constant pool only once.