Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Acess methods

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Acess methods

    could some one please explain to me in laymen terms why access methods are used. I know how to use methods but, I am confused about why access methods are used. I put an example from my book that uses acess methods. I hope this will help someone explain it to me.

    Thanks,
    Truck35

    //This class is used to calculates fuel efficiency
     
    class Vehicle {
    private int passengers;   //number of passengers
    private int fuelcap;   //fuel capacity in gallons
    private int mpg;  // fuel consumption in miles per gallon
     
    //This is a constructor for Vehicle.
     
    Vehicle (int p, int f, int m) {
    passengers = p;
    fuelcap = f;
    mpg = m;
    }
     
    //Return the range.
     
    int range() {
    return mpg * fuelcap;
    }
     
    //Compute fuel needed for a given distance.
     
    double fuelneeded(int miles){
    return (double) miles/mpg;
    }
     
    //These are the access methods  I was talking about.
    //The book says they are for instance variables.
     
    int getPassengers() {return passengers;}
    void setPassengers(int p) {passengers = p;}
    int getFuelcap () {return fuelcap;}
    void getFuelcap (int f) {fuelcap = f;}
    int getMpg() {return mpg;}
    void getMpg (int m) { mpg = m;}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Acess methods

    I think access is an English word and its normal meaning covers what the methods are used for.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Acess methods

    Quote Originally Posted by Truck35 View Post
    ...why access methods are used...
    Since the instance variables (passengers, fuelcap, and mpg) are defined as having "private" access, an application program can not set their values directly. An application program can not retrieve their values directly.

    In other words, the following would not work. (Why not make a test program and try it?).

    /*
      Example of bad code from Zaphod_b.  Compiler error message:
     
     Z.java:11: fuelcap has private access in Vehicle
            System.out.println("Vehicle number 1 has a fuel tank capacity of " + v1.fuelcap + " gallons.");
     
    */
    public class Z {
        public static void main(String [] args) {
            Vehicle v1  = new Vehicle(2, 8, 40);
            System.out.println("Vehicle number 1 has a fuel tank capacity of " + v1.fuelcap + " gallons.");
        }
    }
    This causes a compiler error message as indicated in the opening comment.


    So, a "getter" function allows access to the private instance variables:

    /*
      Example of code using "getter" access function from Zaphod_b.
    */
    public class Z {
        public static void main(String [] args) {
            Vehicle v1  = new Vehicle(2, 8, 40);
            System.out.println("Vehicle number 1 has a fuel tank capacity of " + v1.getFuelcap() + " gallons.");
        }
    }

    Output:
    Vehicle number 1 has a fuel tank capacity of 8 gallons.

    Maybe defining the individual variables to have "private" access and, therefore, requiring "getter" and "setter" access methods, seems to be making extra work. I mean, why not just define them with "public" access? The first program would work "just fine," and you wouldn't need a "getter" access method.

    Well...

    In a little program like this, there might not seem to be much of a downside to having "public" access to the variables, but as programs get bigger and more complicated, there are real advantages to "private" variables. The reasons are covered in every competent introductory discussion of object-oriented programming.

    Bottom line: Get into the habit of making the variables "private" and using "getter" and "setter" methods if the application will need to retrieve or set their values. My observation is that bad habits (like making instance variables public to get an improperly-designed program to work "just fine") formed early in one's development are notoriously difficult to break.

    By the way: I think you have typographical errors where you copied the method names from the text. I'm pretty sure they should be:

        int getPassengers() {return passengers;}
        void setPassengers(int p) {passengers = p;}
        int getFuelcap () {return fuelcap;}
        void setFuelcap (int f) {fuelcap = f;}
        int getMpg() {return mpg;}
        void setMpg (int m) { mpg = m;}

    Cheers!

    Z

  4. The Following User Says Thank You to Zaphod_b For This Useful Post:

    Truck35 (January 29th, 2013)

  5. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Acess methods

    Thanks for the reply. Your reply was awsome.

    Bless,
    Truck

Similar Threads

  1. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  2. I need help with my methods please?
    By THeAnnonymous in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 09:22 AM
  3. Help with methods
    By CSUTD in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 8th, 2011, 10:39 PM
  4. I need help with METHODS!!!
    By Slone in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 14th, 2010, 08:33 AM
  5. Re-using methods?
    By Morevan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2010, 05:04 PM

Tags for this Thread