Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Theory Question

Posted on 2015-02-12
3
Medium Priority
?
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 1000 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 44

Accepted Solution

by:
Chris Stanyon earned 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Progress
Starting up a Project

609 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