0

I have a student object that can hold name and mark.

public class Student{ Public String name; Public int mark; public Student (int argMark, String argName) { mark=argMark; name=argName;} } 

when I create a student as :

 Student john = new Student(70, "John"); 

I need to change the mark later while keeping the previous marks

 Student temp = john; temp.mark = 60; 

when I print them out they both have 60 marks

 System.out.println(temp.mark); System.out.println(john.mark); 

the system shows the answer of : 60 for both of them

I know I can extend a class and have a method for get, set methods and override it, but this is not acceptable for my assignment. Is there any other way to do this?

6
  • Putting aside the uncompilable code for a second, if you need to maintain the old AND new marks for the same instance of the Object, then you should consider using an array or some kind of List. One might also think that the mark should be applied to a subject instead, but that might be taking things beyond the scope Commented Sep 11, 2015 at 2:07
  • so you are suggesting to create an Array of marks ? Commented Sep 11, 2015 at 2:08
  • Assuming you need to maintain more then one mark, for each instance of Student, then yes. But your question lacks some claritity Commented Sep 11, 2015 at 2:09
  • 1
    clone() the student Commented Sep 11, 2015 at 2:09
  • @MadProgrammer : the code above is an example I came up with to ask my question. My program is a little bit confusing so I decided to simplify it. In my program I dont know how many objects my application is getting and it would be terrible to create an array for it cause it would make it so complicating for further calculations Commented Sep 11, 2015 at 2:11

2 Answers 2

1

You can create copy constructor and by doing that you can have new reference with the same attribute values in your temp. Currently John and Temp have same reference and change in one will get reflected in other.

public Student (Student student) { this.mark = student.getMark(); this.name = student.getName(); } 

Few suggestions,

Sign up to request clarification or add additional context in comments.

5 Comments

so i can have both constructors for Student ?
@HiradGorgoroth Yes you can have both constructors as signature of both constructors are different.
Please note that the public Student (Student student) {...} solution would be accepted when the attributes are all immutables, best way would be cloning the object
then if the student.getMark() return an object(not immutable) it will not make sense @TAsk
@TAsk , I will read that. Sorry about the naming, my assignment is a bit more complicating than what I wrote here. I just came up with this example so that others can get what i mean.
1

When you say

Student Temp = John; // <-- typo for John 

you assign a reference to the same instance that Jhon references. Based on your question, you expect a second instance. Something like

Student Temp = new Student(John.Mark, John.Name); 

Also, by convention Java variable names start with a lower case letter.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.