Access Modifiers 05-16-2016, 08:01 PM
#1
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.
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:
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.
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.
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
second class: Shopper
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.javaThe 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
Test.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.
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.
don't have much in the first place, who feel the new currents and ride them the farthest.
















![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: WV5eQ42.jpg]](http://i.imgur.com/WV5eQ42.jpg)