Single Responsibility Principle is first principle from SOLID acronym. A class should not have many tasks to do but it should have only one.
Definition: Every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class, module or function.
That means a class should have one, and only one, reason to change.
A class can be compared to workshop. In workshop every tool can be mixed in the same place or all tools designed for one job can be placed in specific box. Class should be like specialised toolbox and have parts designed for only one job. In class should not be mixed parts for different jobs.
Let’s see this principle in real-life example. Below we can see a simple class which has two simple tasks to do and it is not designed according to single responsibility principle, we will fix this. It has one public method that takes two arguments, one float and one enum. According to these arguments it calculates weight on other celestial bodies and prints the result in console.
public class WeightCalculator {
public void calculateWeight(float weightOnEarth, CelestialBody celestialBody) {
switch (celestialBody) {
case MOON:
System.out.println(weightOnEarth * 0.166 + "kg");
break;
case MERCURY:
System.out.println(weightOnEarth * 0.378 + "kg");
break;
case VENUS:
System.out.println(weightOnEarth * 0.907 + "kg");
break;
case MARS:
System.out.println(weightOnEarth * 0.377 + "kg");
break;
case JUPITER:
System.out.println(weightOnEarth * 2.528 + "kg");
break;
case SATURN:
System.out.println(weightOnEarth * 1.064 + "kg");
break;
case URANUS:
System.out.println(weightOnEarth * 0.889 + "kg");
break;
case NEPTUNE:
System.out.println(weightOnEarth * 1.125 + "kg");
break;
case PLUTO:
System.out.println(weightOnEarth * 0.067 + "kg");
}
}
}
Let’s see UML diagram for this class.

Now we will change above code to be consistent with SRP. We need to delegate two responsibilities to separate classes. First class will be responsible for calculations and second class will be responsible for printing results. Below is the final code.
First we need to create a new class with one public method which takes two arguments, float and enum. Then it calculates new weight and returns it as string.
public class WeightCalculationMaker {
public String makeWeightCalculation(float weightOnEarth, CelestialBody celestialBody) {
switch (celestialBody) {
case MOON:
return String.valueOf(weightOnEarth * 0.166) + "kg";
case MERCURY:
return String.valueOf(weightOnEarth * 0.378) + "kg";
case VENUS:
return String.valueOf(weightOnEarth * 0.907) + "kg";
case MARS:
return String.valueOf(weightOnEarth * 0.377) + "kg";
case JUPITER:
return String.valueOf(weightOnEarth * 2.528) + "kg";
case SATURN:
return String.valueOf(weightOnEarth * 1.064) + "kg";
case URANUS:
return String.valueOf(weightOnEarth * 0.889) + "kg";
case NEPTUNE:
return String.valueOf(weightOnEarth * 1.125) + "kg";
case PLUTO:
return String.valueOf(weightOnEarth * 0.067) + "kg";
default:
return "";
}
}
}
Second class will take responsibility for printing result in console.
public class ResultPrinter {
public void printResult(String result) {
System.out.println(result);
}
}
And finally we have our class which now is consistent with SRP. It has only one responsibility, it delegates tasks to another classes.
public class WeightCalculator {
private final WeightCalculationMaker weightCalculationMaker = new WeightCalculationMaker();
private final ResultPrinter resultPrinter = new ResultPrinter();
public void calculateWeight(float weightOnEarth, CelestialBody celestialBody) {
resultPrinter.printResult(weightCalculationMaker.makeWeightCalculation(weightOnEarth, celestialBody));
}
}
Let’s see updated UML diagram for above modifications.

By using single responsibility principle we gain order in our code which is much easier to maintain. If we need to add new functions we can easily do that without breaking our code.