This question already has answers here:
What does "Can be package local" mean? (IDEA Inspection)
(5 answers)
Closed 6 years ago.
In IDE Some public methods are marked with a warning as Access can be package local and if I remove public form the method no warning will be shown.
Is it a good practice to do so? Should I keep them public anyways ?
If the method is only accessed within the package, the best practice is to use the "default" access modifier (by not specifying any access modifier). Further more briefly here are the four access modifiers used in Java and there accessibility levels.
default- Visible to the package. No modifiers are needed.
private- Visible to the class only.
public- Visible to the world.
protected- Visible to the package and all subclasses.
Related
This question already has answers here:
Why Java allows increasing the visibility of protected methods in child class?
(4 answers)
Closed 5 years ago.
When I try to access and override an inherited abstract function from an abstract class which is of protected access level,
protected <ReturnDataType> <FunctionName>() {
upon removing the Access Type Protected, Eclipse throws me this error
Cannot reduce the visibility of the inherited method from the < inherited class >
which is resolved with the quickfix of giving it the accesstype Protected.
I know that, I can also have the access type as Public for this overrided function.
Now, my question is:: What is the risk / issue with me having this function as Public?
It is a means to help protect your code against errors. There exists coding languages without encapsulation. At their most extreme, any code can change any piece of any data and we may simply not want that to be possible. A disciplined coder will reduce the number of places a given type of data is manipulated, but it may still not be obvious all the combinations of operations that could leave objects* in different states. The situation gets worse when their code is then used as part of someone else's code. So the risks of changing these accessTypes is that it can cause errors down the line if used improperly.
All public does is allow the variable to be accessed through another package, so if someone imported your project as a external library into their project they would be able to access a global variable. This page may be of some use to you: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
This question already has answers here:
Access modifier best practice in C# vs Java
(4 answers)
Closed 5 years ago.
Namely, why is the Java main method public but the C# Main method is defaulted to internal? I also find that classes in C# often don't need to denote an access modifier but in Java we slave over ensuring visibility is restricted just to those that need to see the information of these other classes.
In dotnet the Mainmethod doesn't have to be public. It can be private inside a private class and the runtime will still be able to resolve it. You may want to change the modifiers on Main for your own reasons though.
You can find more detailed information here https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/
In general though dotnet developers do (or should) give the same amount of consideration to access modifiers as Java developers.
This question already has answers here:
What is the difference between public, protected, package-private and private in Java?
(30 answers)
Closed 5 years ago.
I know this might be thought of as a duplicate question, but I think my question is a bit different from the previous questions.
The difference between public and protected members is that a public member acts as a protected member on in that it can be accessed from the world but a protected member cannot.
What does the term world mean? Does it mean from outside the class? If so how?
Is it by using the class name? Or they can be accessed by using the variable/method name directly without the current class being a subclass?
Okay, the word "world" means that it can be accessed from anywhere inside the project (no matter if they are in the same package). However the protected members means that they can only be accessed from other classes inside the same package. Sources
This question already has answers here:
Why does the "protected" modifier in Java allow access to other classes in same package?
(6 answers)
Closed 8 years ago.
I had to shorten the title a bit. Here's the full question:
In Java which access modifier allows a member to be accessed only by the subclasses in other package or any class within the package of that member's class?
I am thinking protected but my office mate says the answer is private.
In Java which access modifier allows a member to be accessed only by the subclasses in other package or any class within the package of that member's class?
see this might help..you ! this is the scope of all access modifiers in java.
Visibility of the properties of class for different access modifiers
——————————————————————————————————————
1) From the same class
Public , Protected, Private and Default
2) From any class in same package
Public, Protected and Default
3) From a subclass in same package
Public, Protected and Default
4) From subclass outside the same package
Public and Protected (by inheritance)
5) From any non-subclass or class outside the package
Public only
It can't be private because private member methods are accessible only within the same class.
protected does not mean that a member will be accessed only by a subclass it specifically means that the classes are accessible within a package.
Your question is either wrong or you missed something on the way of asking your doubt.
Please have a look at the following :
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
This question already has answers here:
What is the difference between public, protected, package-private and private in Java?
(30 answers)
Closed 9 years ago.
Edited to fix being marked as duplicate or to be more clear on why it appears to be a duplicate. At the time I did not know that package and default where the same thus reason for this post.
now I going through exam questions in preparation for my Java exam and I have a question asking me to explain access modifiers and its asking me about a Package modifier.
I can find info on private, Protected, public and default but can't find anything on Package.
can someone please give me an answer or link me to an article about it?
package-private is not a real modifier. You can't type package-private and get the system to recognize it as an access modifier. It's really the default, made by not including any other modifiers.
It means that the given members can only be accessed in the same package.
For example, com.hexafraction.Cow can access a member with default modifiers(none actually) in com.hexafraction.Dog, but com.foo.Crow can't access that member as it's not in the same pacakge.
In this example, the following makes up Cow:
pacakge com.hexafraction;
class Cow{
void moo(){ //no public, protected, or private modifier
System.out.println("moo!");
}
}
Edit for the future: In Java 8, package will supposedly be the modifier needed for this. Literally typing default will still not apply here.
The so-called "package-private" access level is what occurs without a modifier such as private, protected, or public.
Example:
public class Test {
int test; // package-private
}
Anything in the same package, even an unrelated class, can access it, but other classes (even subclasses of the class) outside of the package cannot access it.
This link to the Java tutorial on the subject should help.