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?

Hirad Gorgoroth
  • 389
  • 2
  • 4
  • 13
  • 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 – MadProgrammer Sep 11 '15 at 02:07
  • so you are suggesting to create an Array of marks ? – Hirad Gorgoroth Sep 11 '15 at 02:08
  • Assuming you need to maintain more then one mark, for each instance of `Student`, then yes. But your question lacks some claritity – MadProgrammer Sep 11 '15 at 02:09
  • 1
    `clone()` the student –  Sep 11 '15 at 02: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 – Hirad Gorgoroth Sep 11 '15 at 02:11
  • @HiradGorgoroth Then use some kind of list, see [Collections Trail](http://docs.oracle.com/javase/tutorial/collections/) for more details – MadProgrammer Sep 11 '15 at 02:11

2 Answers2

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,

Community
  • 1
  • 1
akash
  • 22,664
  • 11
  • 59
  • 87
  • so i can have both constructors for Student ? – Hirad Gorgoroth Sep 11 '15 at 02:13
  • @HiradGorgoroth Yes you can have both constructors as signature of both constructors are different. – akash Sep 11 '15 at 02:15
  • 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 –  Sep 11 '15 at 02:19
  • then if the `student.getMark()` return an object(not immutable) it will not make sense @TAsk –  Sep 11 '15 at 02:25
  • 1
    @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. – Hirad Gorgoroth Sep 11 '15 at 02:36
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.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249