Object variable vs setter and getter [on hold]

  • A+
Category:Languages

I would like to ask you: below is class Student, very easy and then below main method. First by example second by me.

public class Student{  private String name; private int age;  public String getName(){     return name; }  public void setName(String name){     this.name = name; }  public int getAge(){     return age; }  public void setAge(int age){     this.age = age; }   } 

And here is main method. I don´t unerstand why making it more complicated. I get the same resultes by using it with my example.

example no.1

public class Main { public static void main(String[] args) {      Student johnReferenceVariable = getStudent("John",25);     System.out.println("Name : = "+johnReferenceVariable.getName());     System.out.println("Age  : = "+johnReferenceVariable.getAge());   }   public static Student getStudent(String name, int age){     Student student = new Student();     student.setName(name);     student.setAge(age);     return student;  } } 

My example of Main class and method:

public class Main { public static void main(String[] args) {      //Student johnReferenceVariable = getStudent("John",25);     Student student = new Student();     student.setName("Martin");     student.setAge(25);     System.out.println("Name : = "+student.getName());     System.out.println("Age : = "+student.getAge());      //System.out.println("Name : = "+johnReferenceVariable.getName());     //System.out.println("Age  : = "+johnReferenceVariable.getAge());   }   //public static Student getStudent(String name, int age){     //Student student = new Student();    // student.setName(name);     //student.setAge(age);     //return student;  //} } 

Thank you much

 


Note that these two lines don't compile:

System.out.println("Name : = "+student.getName()); System.out.println("Age : = "+student.getAge()); 

student is not in scope.

I suppose your question is:

Why should I create a method when I can just write the body of the method?

If you make a method like this:

public static Student getStudent(String name, int age){     Student student = new Student();     student.setName(name);     student.setAge(age);     return student;  } 

you are adding a layer of abstraction. Instead of saying:

Student student = new Student(); student.setName("Tom"); student.setAge(10); 

which is less abstract, you say:

getStudent("Tom", 10); 

which is more abstract.

Abstraction is good, it allows you, and other programmers, to not have to think about what is happening in full detail all the time. When you read your code, sometimes you don't want to know (or you do know already) how your program "gets a student". You may just want to see what your code does on a high level of abstraction.

In this case, your example is too small-scale for this to have an effect. When you are writing much longer methods, you will find the need of extracting some parts of it as methods. This way you don't have to read code at a low level abstraction all the time.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: