Solved

Theory Question

Posted on 2015-02-12
3
113 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 42

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

744 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

11 Experts available now in Live!

Get 1:1 Help Now