Should a method be public or private?
To ensure encapsulation variables should be private and methods public..is that correct?
Declared variables in a method are they private ...?
If I have a method which is private (also the variables are private if my reasoning is correct) , is that an example of encapsulation?
As a general rule of thumb, lower scope is better,
That means, private is better than protected, protected is better than public etc ... Or local variable in a method is better, than a field variable, a field variable is better than a static variable.
So in general the less you expose the less others can depend on it, the converse is obviously you need to expose some detail or then class becomes useless, so finding the balance is what good software design is about.
A method should be public if it needs to be called from outside of the class that contains it, private or protected otherwise. Variables declared within the method are only visible within the scope of that method.
You may want to read more about scope: https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-5035301.html
Usually a variable is private if you don't want someone else to edit it directly. If it doesn't matter if someone else changes the value, you can make it public.
The same thing goes with methods. If its an internal method only, make it private (or protected), otherwise you can make it public. Its all a matter of opinion for the most part though.
Should a method be public or private?
It depends on how it is to be used. Some methods should be private, some public.
To ensure encapsulation variables should be private and methods public..is that correct?
Normally, a variable should be private. When it comes to methods... it depends... See above.
Declared variables in a method are they private ...?
They should be.
If I have a method which is private (also the variables are private if my reasoning is correct) , is that an example of encapsulation?
It can be.
It depends on who can call that method. Generall as a rule you should give the least possible visility to class members. If your class only exists as a companion to some other class in that package then make your class package private and the method package private. If the method is a helper for some other method in the same class, then make it private. If the method is only supposed to be accessed by sub classes and your class is public then make the method protected.
Should a method be public or private?
A method should be private if it is only used by the object it belongs to.
But objects need to communicate with each other, so all methods to this purpose should be public.
Example: Let's say you have an object that controls a web server (a servlet) - call it Servlet. This object communicates with another object named 3DModeler which generates a 3D graphic of an atom. The 3DModeler object can have all kinds of private methods to help itself do the intense calculations, etc. involved in modeling an atom. However, it needs to have a public method to return a reference to the graphic after it models it. It also needs to have a public method that takes parameters regarding the atom so that it can model it in the first place. (e.g., It needs to know the periodic element, if it's an isotope, etc.)
Forgive my chemistry, it sucks, but you get the point :-)
To ensure encapsulation variables should be private and methods public..is that correct?
Yes. (With some exceptions... as is the case for everything. There is even more emphasis on this idea in the Spring framework, where private variables (dependencies) are set through setters/getters.)
Declared variables in a method are they private ...?
They're local to the method. They reside in the stack frame, and when the stack frame is popped they are gone to oblivion. (That is to say, when the method completes, they no longer exist.)
If I have a method which is private (also the variables are private if my reasoning is correct) , is that an example of encapsulation?
Encapsulation is an idea that applies to data - that is why the full word is "data encapsulation". It only makes sense in the context of the data being publicly accessible. The idea is that instead of the data itself being accessible, the data should be private and the way to access it should be through public methods.
The benefit of this is that if the data changes, the interface to the data stays the same, so all the changes to the code are local to the class and don't affect any other classes.
精彩评论