Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9134381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:38:59+00:00 2026-06-17T08:38:59+00:00

I have 3 classes: Course , CourseEntry and Transcript . In transcript, I have

  • 0

I have 3 classes: Course, CourseEntry and Transcript. In transcript, I have a function to add courses, like that:

public class Transcript {
    CourseEntry coursestaken[] = new CourseEntry[6];

    public void addCourse(Course course)
    {
        coursestaken[lastIndexOf(getCoursestaken())] = new CourseEntry(course);
    }
    (lastIndexOf gives me the empty array index - it's working on)

And in my CourseEntry:

public class CourseEntry {
    Course course;
    char grade = 'I';

    public CourseEntry(Course course)
    {
        this.course = course;
    }

And in my Course:

public  class Course {
    int courseNumber,credits;
    String courseName;

    public Course addNewCourse(int courseNumber, int credits, String courseName)
    {
        this.courseNumber = courseNumber;
        this.credits = credits;
        this.courseName = courseName;

        return this;
    }

In my main:

Transcript t = new Transcript();
Course course = new Course();

Course matematik = course.addNewCourse(1, 2, "Matematik");
t.addCourse(matematik);

Course turkce = course.addNewCourse(1, 4, "Türkçe");
t.addCourse(turkce);

But if I loop coursestaken array, it prints the last inserted index for all.

How can I solve that?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:39:01+00:00Added an answer on June 17, 2026 at 8:39 am

    Objects are references in Java, that is, pointers to the objects. So when you do:

    Object a = new Object();
    Object b = a;
    

    You’re NOT copying the whole object a to b, but copying the reference to a to b (the memory address). So both a and b are references to the object created by new.

    Let’s follow your code so you see what’s happening:

    Course course = new Course();
    Course matematik = course.addNewCourse(1, 2, "Matematik");
        this.courseNumber = courseNumber;
        this.credits = credits;
        this.courseName = courseName;
        return this;
    

    Here you modified course object. matematik now is also the same as course because it points to same object.

    Course turkce = course.addNewCourse(1, 4, "Türkçe");
    

    Here you modify course again. Now course, turkce and matematik are all referencing the same object, which you created first with Course course = new Course();.

    I think most easy way to fix this is that you create a constructor with parameters:

    public class Course {
    ...
         public Course(int courseNumber,int credits,String courseName) {
               this.courseNumber = courseNumber;
               this.credits = credits;
               this.courseName = courseName;
         }
    }
    

    and then

      Course matematik = new Course(1, 2, "Matematik");
      t.addCourse(matematik);
    
      Course turkce = new Course(1, 4, "Türkçe");
      t.addCourse(turkce);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes: public class CourseModule { // attributes... List<Course> courses; public void
I have classes like this one: class SomeObject { public function __construct($param1, $param2) {
I have classes structured like this: Public MustInherit Class A ' several properties End
Suppose you have 2 classes like so: public class ClassA { public int X
I have classes which have automatic properties only like public customerName {get; set;}. They
If I have classes of Type A and B: public class A { public
Lets say we have these classes: class Teacher { public int Id { get;
I have a function that populates an object using IDataReader: private void FillDataItem(IDataReader datareader)
I have 3 classes (simplified for this question): public class Page { public Guid
I have a Serializer class like this: class Serializer { public: // Func 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.