Solved

Theory Question

Posted on 2015-02-12
3
117 Views
Last Modified: 2015-02-12
Greetings,

I am working through someone else's code and learning as I go.  I came across this pice of code and I wonder why would I write this?

private static int EXPECTED_LENGTH_OF_BYTE_ARRAY = 998;
        public static int expectedLengthOfBHMConfigurationArray
        {
            get
            {
                return EXPECTED_LENGTH_OF_BYTE_ARRAY;
            }
        }

Instead of calling expectedLengthOfBHMConfigurationArray and having a 998 returned would it not be more efficient to reference the variable EXPECTED_LENGTH_OF_BYTE_ARRAY directly?
0
Comment
Question by:RonWensley
3 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 250 total points
ID: 40605255
For reading - yes.
BUT if you look very carefully there is no way an external can modify this value.  If it was a public variable then other parts of the program could modify the value with possibly disastrous effects.
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 250 total points
ID: 40605337
One of the key principles of Object Oriented code is encapsulation. As Microsoft puts it:

This principle states that data stored inside an object should be accessible only to that object.

Your EXPECTED_LENGTH_OF_BYTE_ARRAY variable is a private field and is only accessible inside the class. It is then exposed to the outside world via a public property - expectedLengthOfBHMConfigurationArray in your case.

This means that consumers of your class will get to the value via a public property. You have the flexibility to change the implementation of that property within your class without breaking the code of the consumers.
0
 

Author Closing Comment

by:RonWensley
ID: 40605676
Both answer were informative thank you for them.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now