Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Getters/Setters controversy

807603Dec 5 2007 — edited Dec 9 2007
So I'm going back and forth about whether or not to use getters and setters and any alternatives if I don't. I've read some articles that say getters and setters should be avoided at all cost.
But I have an example program here, with a class called FlightInfo which basically holds all of the flight information such as flight date and the flight number. Wouldn't it be easiest to use getters and setters like the following:
public class FlightInfo {
        private String date;
        private int flightNumber;
        
        public String getDate(){
                return date;
        }

        public setDate(String date){
                this.date = date;
        }

        public int getFlightNumber(){
                return flightNumber;
        }

        public setFlightNumber(String flightNumber){
                this.date = flightNumber;
        }
}
Is there any better alternative to this? Also, the flight date may change for a particular flight number.
Thanks.

Edited by: java_fan_69 on Dec 5, 2007 10:33 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 6 2008
Added on Dec 5 2007
186 comments
422 views