Sinisterly
Access Modifiers - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Java, JVM, & JRE (https://sinister.li/Forum-Java-JVM-JRE)
+--- Thread: Access Modifiers (/Thread-Access-Modifiers)



Access Modifiers - Inori - 05-16-2016

Access modifiers are useful for distributed programs, since some (or all) instance variables are exactly set to the programmer's preference, and changing them could spell disaster for the program. Other examples could include confidential information such as card numbers or passwords.

In this tutorial, I'll explain how to use access modifiers to your safety and advantage.

Public

Public is the most common modifier, and probably the one you'll use most when making programs that don't need to be secure and don't have volatile information. While this is accepted by Java, it's highly recommended that you do not use public for everything.

A class, method, or variable with the public modifier is globally accessible. This means that any method in any class across the JVM can read and (if it's not final, which we'll discuss later) write to it.

Spoiler: example
First class: Store
Code:
// class header public class Store{ // public class variable public String[] items; // class constructor public Store(String[] items){ // note the same modifier as the class // set the instace of items to the argument this.items=items; } }

second class: Shopper
Code:
// class header public class Shopper{ // public class variable public String cart; // get the items public getItems(Store shop,String item){ // see if the store has the item by going through the shop's items for(String shopItem:shop.items){ // if the items match... if(item.equals(shopItem)){ // set the cart instance to the item this.cart=item; } } } }


Protected

The protected modifier is for larger, class- and package-structured (folder) projects that need access to certian class attributes that shouldn't be access outside of their packages. When in the same package, the protected modifier is essentially public. See the below directory tree:
Code:
./ ├──C.java └──sysPack ├─A.java └─B.java

The classes A and B can access any public and protected attributes of each other, while the C class can only access the public ones.

Another thing about this package accessing is that protected class attributes can be accessed outside of their packages via inheritance (the extends keyword), but protected classes can't be accessed outside their package by any means.

A final note about protected: not using an access modifier (i.e., "class A" as opposed to "public class A") defaults to protected, but it's convention to write it either way.

Private

The private modifier denotes a class attribute that can be neither read nor written by any class that isn't the one it belongs to. This said, private is the only modifier than can't be used for classes, for obvious reasons.

Unlike protected, private class attributes can't be sneaked out through the use of the inheritance trick.

Final

The final modifier isn't really an access modifier, since it can be (and usually is) used in conjunction with public, private, or protected, but it denotes immutability, or the inability to change. This means that even when defined but not initialized, you cannot set the value of a final variable.

Static

Static follows the same convention as final in the sense that it isn't really an access modifier. It's function, however, is turning a variable into one shared across all instances of a class. See the example for a practical explanation.

Spoiler: example
Counter.java
Code:
// class header public static class Counter{ // static, shared integer public static int c=0; // constructor public Counter(){ // increment c by 1 c++; } // print c public void print(){ System.out.println(c); } }

Test.java
Code:
// class header public class Test{ // main method public static void main(String[] args){ // create new counter instances Counter a=new Counter(),b=new Counter(); // print c a.print(); } }




That's about it. I can't think of much else anyone would need to structure a java project, but if anyone thinks of anything, feel free to PM me and I'll add it and credit you.


RE: Access Modifiers - Xiledcore - 06-11-2016

Thanks for putting in the effort to educate people on access modifiers. :p

Here are just a few thoughts:
- Preferrably, you'd want every beginner in Java to start using the private access modifier whenever possible, instead of the public one. As you've already mentioned, it's bad practice to use public everywhere, and in fact, the access scope should be as narrow as possible for limited mutability.
- When not supplying a modifier, it defaults to the package-private modifer, not protected. Although the two are quite similar, the protected also enables for a subclass to access the variable, whereas the package-private does not (unless the subclass is in the same package).
- You can't declare top-level classes as static as they are basically static by default anyway, so putting the static keyword there would be quite redundant.

Other than that, you explained it well!


RE: Access Modifiers - Inori - 06-11-2016

(06-11-2016, 06:34 PM)Xiledcore Wrote: Thanks for putting in the effort to educate people on access modifiers. :p

Here are just a few thoughts:
- Preferrably, you'd want every beginner in Java to start using the private access modifier whenever possible, instead of the public one. As you've already mentioned, it's bad practice to use public everywhere, and in fact, the access scope should be as narrow as possible for limited mutability.
- When not supplying a modifier, it defaults to the package-private modifer, not protected. Although the two are quite similar, the protected also enables for a subclass to access the variable, whereas the package-private does not (unless the subclass is in the same package).
- You can't declare top-level classes as static as they are basically static by default anyway, so putting the static keyword there would be quite redundant.

Other than that, you explained it well!

All very insightful except for the last point. Top-level classes are by convention not static, but instantiable objects.


RE: Access Modifiers - Xiledcore - 06-11-2016

(06-11-2016, 07:26 PM)Emilia Wrote:
(06-11-2016, 06:34 PM)Xiledcore Wrote: Thanks for putting in the effort to educate people on access modifiers. :p

Here are just a few thoughts:
- Preferrably, you'd want every beginner in Java to start using the private access modifier whenever possible, instead of the public one. As you've already mentioned, it's bad practice to use public everywhere, and in fact, the access scope should be as narrow as possible for limited mutability.
- When not supplying a modifier, it defaults to the package-private modifer, not protected. Although the two are quite similar, the protected also enables for a subclass to access the variable, whereas the package-private does not (unless the subclass is in the same package).
- You can't declare top-level classes as static as they are basically static by default anyway, so putting the static keyword there would be quite redundant.

Other than that, you explained it well!

All very insightful except for the last point. Top-level classes are by convention not static, but instantiable objects.

A top-level class can be considered static because it can be referred to without  an instance being needed to do so. You might have confused the syntax with the C# syntax in which static classes are allowed. Only static nested classes are allowed in Java.