Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of setters and getters in java? [duplicate]

Please forgive the length, but here are two programs, both the exact same, but one with and one without setters, getters, and constructors.

I've taken a basic C++ class before and don't remember any of these from it, and at the moment I'm not seeing the point of them, if anyone could explain them in lamen's terms I'd much appreciate it...at the moment they seem to be nothing more than space wasters to make my code look longer, but the teacher says they are important (and so far that's it).

Thanks in advance! And now here's the code: Mileage.java:

package gasMileage;  import java.util.Scanner; //program uses class Scanner  public class Mileage  {     public int restart;     public double miles, gallons, totalMiles, totalGallons, milesPerGallon;     public Mileage(int newRestart, double newMiles, double newGallons,                     double newTotalMiles, double newTotalGallons, double newMilesPerGallon)     {         setRestart(newRestart);         setMiles(newMiles);         setGallons(newGallons);         setTotalMiles(newTotalMiles);         setTotalGallons(newTotalGallons);         setMilesPerGallon(newMilesPerGallon);     }     public void setRestart(int newRestart)     {         restart = newRestart;     }     public int getRestart()     {         return restart;     }     public void setMiles(double newMiles)     {         miles = newMiles;     }     public double getMiles()     {         return miles;     }     public void setGallons(double newGallons)     {         gallons = newGallons;     }     public double getGallons()     {         return gallons;     }     public void setTotalMiles(double newTotalMiles)     {         totalMiles = newTotalMiles;     }     public double getTotalMiles()     {         return totalMiles;     }     public void setTotalGallons(double newTotalGallons)     {         totalGallons = newTotalGallons;     }     public double getTotalGallons()     {         return totalGallons;     }     public void setMilesPerGallon(double newMilesPerGallon)     {         milesPerGallon = newMilesPerGallon;     }     public double getMilesPerGallon()     {         return milesPerGallon;     }     public void calculateMileage()     {         Scanner input = new Scanner(System.in);         while(restart == 1)         {             System.out.print("Please input number of miles you drove: ");             miles = input.nextDouble();             totalMiles = totalMiles + miles;             System.out.print("Please input number of gallons you used: ");             gallons = input.nextDouble();             totalGallons = totalGallons + gallons;             milesPerGallon = miles / gallons;             System.out.printf("Your mileage is %.2f MPG.\n", milesPerGallon);             System.out.print("Would you like to try again? 1 for yes, 2 for no: ");             restart = input.nextInt();         }         milesPerGallon = totalMiles / totalGallons;         System.out.printf("Your total mileage for these trips is: %.2f.\nYour total gas consumed on these trips was: %.2f.\n", totalMiles, totalGallons);         System.out.printf("Your total mileage for these trips is: %.2f MPG", milesPerGallon);     } } 

Mileagetest.java:

package gasMileage;  public class Mileagetest  {     public static void main(String[] args)      {         Mileage myMileage = new Mileage(1,0,0,0,0,0);         myMileage.calculateMileage();     } } 

And now for the one without setters and getters:

Testmileage.java:

package gasMileage;  import java.util.Scanner;  public class Testmileage  {     int restart = 1;     double miles = 0, milesTotal = 0, gas = 0, gasTotal = 0, mpg = 0;     Scanner input = new Scanner(System.in);     public void testCalculate()     {         while(restart == 1)         {             System.out.print("Please input miles: ");             miles = input.nextDouble();             milesTotal = milesTotal + miles;             System.out.print("Please input gas: ");             gas = input.nextDouble();             gasTotal = gasTotal + gas;             mpg = miles/gas;             System.out.printf("MPG: %.2f", mpg);             System.out.print("\nContinue? 1 = yes, 2 = no: ");             restart = input.nextInt();         }             mpg = milesTotal / gasTotal;             System.out.printf("Total Miles: %.2f\nTotal Gallons: %.2f\nTotal MPG: %.2f\n", milesTotal, gasTotal, mpg);     } } 

Testmileagetest.java:

package gasMileage;  public class Testmileagetest  {      /**      * @param args      */     public static void main(String[] args)      {         Testmileage test = new Testmileage();         test.testCalculate();     }  } 

Thanks again!

like image 291
Soully Avatar asked Sep 22 '09 17:09

Soully


People also ask

What was the point for setter and getter Java?

Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value.

What will happen if getters and setters are made private?

The reason for declaring the getters and setters private is to make the corresponding part of the object's abstract state (i.e. the values) private. That's largely independent of the decision to use getters and setters or not to hide the implementation types, prevent direct access, etc.

What is the point of setter methods?

The method that is used to set/modify the value of a private instance variable of a class is known as a setter method and, the method that is used to retrieve the value of a private instance variable is known as a getter method.

What is the advantage of using getters and setters that only get and set instead of simply using public fields for those variables?

1) getter and setter method gives you centralized control on how a the particular field is initialized and provided to the client which makes the validation and debugging much easier. you can simply put breakpoints or print statement to see which thread are accessing and what values are going out.


1 Answers

The point of getters and setters, regardless of language, is to hide the underlying variable. This allows you to add verification logic when attempting to set a value - for example, if you had a field for a birth date, you might only want to allow setting that field to some time in the past. This cannot be enforced if the field is publicly accessible and modifyable - you need the getters and setters.

Even if you don't need any verification yet, you might need it in the future. Writing the getters and setters now means the interface is kept consistent, so existing code won't break when you change it.

like image 136
Michael Madsen Avatar answered Sep 29 '22 09:09

Michael Madsen