Why private variables, while still can be accessed by accessor methods

Agreed it is well defined way of encapsualtion. But i wonder how it works differently. can any one explain . We declare a varibale as private which can be accessed by accessor (set/get) methods which are usually public. so why dont we say vairbkles itself as Private ??
Pls explain

Not only that, but imagine the case where you have an api method, getTotal(). Your first iteratation you have something like:
double gross;
double net;
double commission;
so to get the total you may want gross - commission = net. Thus your method may look like:
public double getTotal()
return gross - commission;
But later on, iteration 2 or so, you find out you need to account for taxes, so now you don't want to break the code, the result should be the same, the NET total. However, you now need to account for taxes:
public double getTotal()
return (gross - commission) + tax;
Without breaking any code and all while maintaining the actual API definition, you have made a public accessor method provide more functionality. All code using the getTotal() method wont break nor has to be recompiled and even better it now takes into account tax.
Now, there is one "sticky" issue in the above. Because the getTotal() makes direct use of "private" variables, you may not get the "right" tax. In other words, what if there is state and local tax that need to be added? You may have two variables:
double localTax,stateTax;
The problem is, do you want your method like this:
public double getTotal()
return (gross - commission) + localTax + stateTax;
Or would it look (and possibly account for future updates) better like so:
return (gross - commission) + getTax();
The thing is, what if later on you have additional tax to handle? Sure, you can simply add it to the end, adding it, what ever. But by making a "helper" method of getTax(), you might have on iteration 2:
private double getTax()
return localTax + stateTax;
and in iteration three, you may have:
private double getTax()
return localTax + stateTax + salesTax;
Now, unless you are calling code in an intensive performance needy loop, making a couple of helper method calls won't hurt performance at all, and it will greatly enhance your code quality because you can easily change the implementation of the method(s) used, without affecting the API.

Similar Messages

  • I have been with google for while now but I still can't access to google wallet, Would you please show me how? thanks

    I HAVE BEEN WITH GOOGLE FOR WHILE NOW BUT I STILL CAN'T ACCESS TO MY GOOGLE WALLET, BOTH ACCOUNTS, PERSONAL AS WELL AS BUSINESS. WOULD YOU PLEASE SHOW ME HOW? THANKS

    Seems like that would be a better question for a Google forum since it it Google's app. Here is a link about it from Google:
    https://www.google.com/wallet/
    If you need further help, I would suggest you post to a Google forum.
    GB

  • Hi, I can't seem to get my ipod 32gb 4th gen to sync with itunes. It's brand new, but everytime I plug it in it says can't access Itunes store and nothing else happens. I enabled my firewall to allow itunes but it still can't access it. Please help.

    Hi, I can't seem to get my ipod 32gb 4th gen to sync with itunes. It's brand new, but everytime I plug it in it says can't access Itunes store and nothing else happens. I enabled my firewall to allow itunes but it still can't access the store. Please help.

    If you look at the feed, and look at the entry 'The end to an exciting field season in Aulavik National Park' you will see the line:
    <pubDateFri, 29 Jul 2011 04:00:00 GMT</pubDate>
    You have not closed the 'pubdate' tag - there should be a '>' as the 9th character. Because the tag has been left open, it invalidates everything which follows it, and thus the entire feed. iTunes therefore cannot read it, which is why the Store has not updated. Add the '>' and republish, and all should be well.
    It's always worth checking your feed by subscribing manually, from the 'Advanced' menu, or by clicking the 'subscribe free' button on the Store page (both do the same thing) - if you can subscribe to the feed it should be OK.

  • I purchased microsoft office mac in 2004 and suddenly I get an error message. It won't launch. I click reopen and it does the same thing. I get a long message in the panel. It tells me it will send a report. I still can't access any microsoft item - word

    I purchased microsoft office mac in 2004 and suddenly I get an error message. It won't launch. I click reopen and it does the same thing. I get a long message in the panel. It tells me it will send a report. I still can't access any microsoft item - word, excel, nothing works.
    I have a mac book pro 10.6.8
    Thanks

    cassidy3 wrote:
    Good news is that I can access all my documents. Bad news is I had to pay $150 for it.
    I spent a lot of time trying to find a solution before buying.  I did not try the launch terminal solution....
    I will check back so I can find out what to do should this happen again.
    More possible bad news.  If you had followed what fane_j had asked you to do you might have found out it wasn't microsoft's fault for your problem and wouldn't have had to spend $150.  Because while you may have an Office that works, fane_j was not suggesting a solution but probing for a trojan (google "flashback trojan") that has been crashing powerpc code which is what Office 2004 is.
    So if you do have the trojan, while you can run a version of Office, the trojan may still be in your system since installing a version of Office isn't going to remove it.  So I'll make another request.  Launch Terminal (in your Utilities folder) and copy/paste the following line into the terminal window and hit return:
    defaults read ~/.MacOSX/environment
    If you see anything other than a "does not exist" error message you almost certainly do have the trojan and post the results in this thread.
    The choice is yours.  You can ignore this and take the risk.  Or do what has been requested.

  • I have enabled cookies, but I still can't access the website. HELP!

    I have enabled cookies, but I still can't access the website. I keep getting a pop up asking me to enable cookies. How do I fix this?? See screenshots. HELP!!!

    See also:
    *http://kb.mozillazine.org/Websites_report_cookies_are_disabled
    Can you post a link to a publicly accessible page (i.e. no authentication or signing on required)?
    You can also check if DOM Storage is enabled.
    *http://kb.mozillazine.org/dom.storage.enabled
    You can check to make sure that you do not run Firefox in permanent Private Browsing mode (Never remember history).
    *https://support.mozilla.org/kb/Private+Browsing
    Start Firefox in <u>[[Safe Mode|Safe Mode]]</u> to check if one of the extensions (Firefox/Tools > Add-ons > Extensions) or if hardware acceleration is causing the problem.
    *Switch to the DEFAULT theme: Firefox/Tools > Add-ons > Appearance
    *Do NOT click the Reset button on the Safe Mode start window
    *https://support.mozilla.org/kb/Safe+Mode
    *https://support.mozilla.org/kb/Troubleshooting+extensions+and+themes
    See also:
    *https://support.mozilla.org/kb/how-do-i-create-screenshot-my-problem
    *Use a compressed image type like PNG or JPG to save the screenshot
    *Make sure that you do not exceed the maximum size of 1 MB
    You can also try to crop images and only keep the relevant part(s) and possibly save them with a higher compression value.

  • When I click the bookmark icon on the Google tool bar I am asked to login which I do but still can't access my bookmarks.

    My OS is Vista Business and I'm using Firefox version 8. I'm using the Google Tool Bar and have an icon for Bookmarks but when I click it a message pops up "Sign in to access your Google bookmarks. I sign in but still can't access my bookmarks.

    The Google Toolbar isn't compatible with versions of Firefox beyond Firefox 4. If you are overriding extension compatibility to get to be enabled in Firefox 8, that isn't going to work since Google changed the logon URLs a couple of months ago; that broke the Google Toolbar even further.

  • I'm trying to update a Muse site and it keeps telling me that the site was created with a different version of Muse. I have updated Muse and still can't access the site to make changes.

    I'm trying to update a Muse site and it keeps telling me that the site was created with a different version of Muse. I have updated Muse and still can't access the site to make changes.

    Hi rgarden95,
    Can you please confirm the exact version of Muse you are opening the file with?
    To check the version of Muse, please click on help->About Adobe Muse CC.
    Regards,
    Akshay

  • HT4906 I have upgraded my OS to Mountain Lion and updated Iphoto to latest, however, I still can not access photos sent to me via photo stream. what else do I need to do? I'm $30 into this effort, and no results!

    I have upgraded my OS to Mountain Lion and updated Iphoto to latest, however, I still can not access photos sent to me via photo stream. what else do I need to do? I'm $30 into this effort, and no results!

    Log out of iCloud in the System/iCloud preference pane and disable Photo Stream in iPhoto's Photo Share preference pane.  Then log back in and re-enable, respectively.  That should jump start Photo Stream.
    OT

  • Hello! I cant't access home on my IPad after update 7.1.2 .. I slide to open it but i get an empty screen then it turned off and turn on, but still can't access my IPad, please help!!

    hello! I cant't access home on my IPad after update 7.1.2 .. I slide to open it but i get an empty screen then it turned off and turn on, but still can't access my IPad, please help!!

    I had the same problem but looking at what everyone else said, I simpley slowly started to click on all the tabs in the app store, let them load completely, and then clicked back to the updates afterwards. It worked after a little while

  • I've followed all the instructions to the letter but still can't access the signatures in my Mail account. Any ideas?

    I've followed all the instructions to the letter but still can't access the signatures in my Mail account. Any ideas?

    Hello,
    In Mail Preferences>Signatures, what shows for All Sigs, And the ones assigned to a particular account?
    Then in Compose a new Mail, what shows for Sig choices?

  • Ting screwy. I deleted the app and reset my ipod but I still can't access either Itunes stores, send messages, or access the internet. What should I do?

    I downloaded nds4ios for my ipod touch and now it's acting screwy. I deleted the app and reset my ipod but I still can't access either Itunes stores, send messages, or access the internet. What should I do?

    Hi Seabiscuit564,
    It sounds as if your issues here are all related to not having a good connection to the Internet from your iPod touch. I would suggest that you troubleshoot using the steps in this article - 
    iOS: Troubleshooting Wi-Fi networks and connections
    Thanks for using Apple Support Communities.
    Best,
    Brett L 

  • HT5312 No matter how many times I reset my password, I still can't access my account. PLEASE HELP. :(

    I had to make this new Apple ID because I can't access my old one.
    I forgot my password to my original Apple ID so I followed all the instructions on iforgot.apple.com
    After I got the email confirmation saying that my password was successfully reset, I tried out my new password but it kept saying 'Your AppleID or password was entered incorrectly.'
    I did this numerous times before. I even tried to get help from Apple Support through email but no matter what I did, it was useless. Please, anybody, help, I'd still like to use my old Apple ID. Thanks.

    I seen this many times, and in just about all cases, it's the user resetting a different AppleID than what's being used for the iTunes account.

  • HT4914 Once I turn on Match, how do I remove the songs from my iPhone, thus freeing up the memory, which is my initial purpose anyway? (while still having instant access to all my music)

    Once I turn on Match, how do I remove the songs form my iPhone, thus freeing up the memory, which is my initial purpose anywhy (while still having complete instant access to all my music) ?

    Hello there dwflemming1,
    I recommend turning off iTunes Match on the computer and the phone. Then connect the phone to the computer and uncheck the box to sync music, then click apply in the lower right corner of iTunes. When that completes, turn match back on on both devices.
    iOS: Syncing with iTunes
    http://support.apple.com/kb/HT1386
    And just in case, iTunes: How to add a computer or iOS device to iTunes Match
    http://support.apple.com/kb/ht4913.
    Cheers,
    Sterling

  • I have a 3g micro sim for my ipad but i still can't access the internet?

    Hi there,
    I have an ipad 2 and my micro sim just arrived to use 3G internet, however when I put the sim into the ipad I still have NO ACCESS to the internet. Please help!

    Is it a different network? Settings: Cellular Data : APN Settings  If you have changed networks then it will need the new network's settings which can be manually entered or by hooking up to iTunes.

  • TS4036 I cant access my Itune store from my PC. I have entered my ID but still can't access the store. I have tried removing it and re-installing it but to no avail. I can access the Itunes store with Iphone and Ipad but not my PC.

    I can't access my itune store through my PC. I have tried removing and reinstalling Itunes,but to no avail. I also tried accessing trough I cloud on my pc but it say apple ID not correct but my Apple ID works on all my other devices such as Iphone 4s and Ipad 2. If I go to Icloud.com I can access my account but can't get to Itune store.
    Help
    Thanks
    Dan

    The apps I currently have are for the i phone and i pad but i would like to see this on this computer
    On the Mac Air launch iTunes. Then connect either the iPad or iPhone.
    Select the iPad or iPhone under Devices left side of the iTunes window in the source list.
    Then select Apps from the menu on the right.
    If you don't see the apps listed there, from your iTunes menu bar (top of your screen), click Store > Authorize This Computer.
    See if that makes a difference...

Maybe you are looking for