1 / 4

Getter and Setter Methods

Getter and Setter Methods. Setter methods public methods that set the value of instance variables to a value specified by the call to the argument Setter methods do not violate the idea of private data you only change the variables you want Returns void (i.e. nothing) Getter methods

danika
Download Presentation

Getter and Setter Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Getter and Setter Methods • Setter methods • public methods that set the value of instance variables to a value specified by the call to the argument • Setter methods do not violate the idea of private data • you only change the variables you want • Returns void (i.e. nothing) • Getter methods • public method that displays value of private variables • Again, does not violate idea of private data • You only display values you want to display • Also called “accessor” or “query” methods • Other objects can treat the object as a black box

  2. class carSetter { private String licensePlate; private int speed; void setlicensePlate(String plate) //setter method { licensePlate = plate; } void setspeed(intispeed) //setter method { speed=ispeed; } }//end class

  3. class carSetter { private String licensePlate; private int speed; void setlicensePlate(String plate) //setter method { licensePlate = plate; } void setspeed(int ispeed) //setter method { speed=ispeed; } String getlicensePlate() //getter method { return licensePlate; } int getspeed() //getter method { return speed; } }//end class

  4. class getter_Example { public static void main(String args[]) { carSetter a = new carSetter(); a.setlicensePlate("02 C 14"); a.setspeed(10); System.out.println(a.getlicensePlate() + " is moving at " + a.getspeed() + " mph."); }//main }//

More Related