1

I am trying to understand java class and object relation but facing one problem.

Here I have one simple java bean class:

public class Student {

    int id;
    String name;
    String marks;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMarks() {
        return marks;
    }

    public void setMarks(String marks) {
        this.marks = marks;
    }

}

And one Editor class:

public class EditStudent {

    public static void editStd(Student st){

        st.setId(10);
        st.setName("editAbleName");
        st.setMarks("133");


       // return st;
    }

I am writing this logic for student object in my program class:

public class TestProgram {

    public static void main(String args[]){
        System.out.println("main");

        Student std = new Student();
        std.setId(1);
        std.setName("zeeshan");
        std.setMarks("44");

        EditStudent.editStd(std);

        System.out.println("id " +std.getId());
        System.out.println("name " +std.getName());
        System.out.println("marks " +std.getMarks());


    }
}

Output:

main
id 10
name editAbleName
marks 133

I supposed that I will get output values of my object that I set on my test program class but getting output values of those which I set on my EditStudent class. Why this happen even I think there is no relation between my std object and editStudent Class ? Please explain me logic occurs behind this logic and process!

4 Answers4

2

std is not an Object. It is a reference to an Object. So, when you pass it to the method editStd, you pass the reference to the Object.

Therefore, std and st refer to the same object(as they store the same reference) Thus, any changes to the object pointed by st, is also reflected in std, as they are the same.

dumbPotato21
  • 5,353
  • 5
  • 19
  • 31
1

EditStudent.editStd(std);-- Even if you are passing modified object of std in editStd method, editStd again modifies std object with values in the method. This is because std and s object refers to same object on heap.

davidxxx
  • 104,693
  • 13
  • 159
  • 179
0

your EditStudent class will get inherit through your argument of student class (st) you have made. You don't need to define the values of Base class (student) in EditStudent class. your testprogram class is correct then it will override the values of EditStudent Class and you will get the output of your derived class(testprogram). So remove the values from EditStudent Class.

Mohini Galhotra
  • 29
  • 1
  • 10
0

You are passing a reference to the Object.

In Edit student

 EditStudent.editStd(std);  // In java It is a reference 
     // IN C++ You used to write '&' in your method(function)
  but in Java it is not like that. Java doesn't have pointers --> either

Since you are new I will tell you how to write the class properly

public class Student {
         // Make them PRIVATE
private int id;
private String name;
private String marks;

    // create constructor
         Student(){}  // empty
         Student(int id, String name, Strings marks){
         this.id = id;
         this.marks = marks;
         this.name = name;} // overloaded, If you want

// use Getter and setter for only Private field.. NO need for Public

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getMarks() {
    return marks;
}

public void setMarks(String marks) {
    this.marks = marks;
}

}

Benefit of overloaded constructor public class TestProgram {

public static void main(String args[]){
    System.out.println("main");

/*    Student std = new Student();
    std.setId(1);
    std.setName("zeeshan");
    std.setMarks("44");  */

 // benefit of overloaded constructor
       Student s = new Student(1,"zeeshan", 44);  
       // you only need one line

//    EditStudent.editStd(std);      // Here you are passing the reference to your object to another class.. Which changes the original 

// If you want to edit student Make a method in Student class
 // I am following your logic
  // create an empty student and pass it.
    Student s2 =    EditStudent.editStd(   new Student() );
           // return the student from editstd goto method it is below
    System.out.println("id " +std.getId());
    System.out.println("name " +std.getName());
    System.out.println("marks " +std.getMarks());


}

}

 public class EditStudent {

   public static Student editStd(Student st){
       // return type student 
      st.setId(10);
      st.setName("editAbleName");
      st.setMarks("133");


      return st;
  }
Rohaitas
  • 84
  • 1
  • 10