简体   繁体   中英

Can we use Setter Method in java to perform operations?

Is setter method only use to assigning values? or can we perform operations in it. Here in this code the commented part is giving me correct output but while using set and get I am getting output as 0. I want to avoid calling totalMarksOfStudent() method again and again because it have 5 parameters which I dont want to give again and again. So what is the way to return totalMarksStudent in another class without calling totalMarksOfStudent().

int totalMarksStudent = 0;


public void setMarks(int englishMarks, int mathsMarks, int physicsMarks, int chemistryMarks, int csMarks) {
    totalMarksStudent = englishMarks + mathsMarks + physicsMarks + chemistryMarks + csMarks;
}

public int getMarks(){
    return totalMarksStudent;
}

   // public int totalMarksOfStudent(int englishMarks, int mathsMarks, int physicsMarks, int chemistryMarks, int csMarks) {
   // totalMarksStudent = englishMarks + mathsMarks + physicsMarks + chemistryMarks + csMarks;
   // return totalMarksStudent;

}
public String displayTotalMarks() {
    String totalMarks1 = "Name " + name + "\tRoll No " + rollNo + "\tTotal Marks " + getMarks();//totalMarksOfStudent(englishMarks, mathsMarks, physicsMarks, chemistryMarks, csMarks);
    return totalMarks1;
}

Better to avoid that...

I think it's better to have some fields like your parameters in setMarks (englishMarks , mathsMarks , ...) , and give value to them in constructor or setter methods. Also it's better to have a method named something like calculateTotalMarks , and call it without any parameters whenever you need it. Remember that there will be no problem to have operations in setter methods but usually and for better designed program we avoid that. Methods should do the thing their name says : for example , setter just for assigning , getter just for accessing values , calculateTotalMarks for calculating the total marks and so on ...

setter method is usually used to assigning values. It is promise. You can reduce parameters by using Object I recommend to make object of MarksStudent. because common attribute can bind to one class. It make understand easily code

for example

// Java is object-oriented language
class marksStudents {
    private int english;
    private int math;
    private int physics;
    private int chemistry;
    private int cs;



    //getMethods is Abbreviation

    public int getTotal() {
        return english+math+physics+chemistry+cs;
    }
    //setMethods
    public void setEnglish(int english) {
        this.english = english;
    }

    public void setMath(int math) {
       this.math = math;
    }

    public void setPhysics(int physics) {
        this.physics = physics;
    }

    public void setChemistry(int chemistry) {
        this.chemistry = chemistry;
    }

    public void setCs(int cs) {
        this.cs = cs;
    }
}

To execute

public class Main{
    public static void main(String[] args) {
        // You can make object marksStudents of studentsA
        marksStudents studentsA = new marksStudents();
        studentsA.setChemistry(20);
        studentsA.setEnglish(30);
        studentsA.setMath(40);
        studentsA.setCs(50);
        studentsA.setPhysics(60);
    
        //200
        System.out.println(studentsA.getTotal());
    
        // You can make object marksStudents of studentsB too
        marksStudents studentsB = new marksStudents();
        studentsB.setChemistry(10);
        studentsB.setEnglish(10);
        studentsB.setMath(10);
        studentsB.setCs(10);
        studentsB.setPhysics(10);
    
        //50
        System.out.println(studentsB.getTotal());
    
    }
}

The getter/setter method is only a practice. Not bad practice - it just defines a class, whose instances for the external world are handled by a list of independent values. Using them makes your code better comprehensible and easy to understand, what is it doing.

So it is no problem to make other operations with it, in general.

Some frameworks like to use reflection to use getters/setters and also reach the variables directly in them. In these cases, doing any different in the getters/setters than reading/writing the private members is no wise idea. Sometimes you can use a little bit of api/impl interface trickery to handle this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM