Using the private access modifier seems pretty useless to beginner programmers. It's not as useless as you think.
Assuming you know how objects work, lets create a small program:
Code:
public class Main {
public static void main(String[] args) {
Person person = new Person(20);
}
}
class Person {
public int age;
public Person(int a) {
age = a;
}
}
In this simple program, we create 1 instance of Person, setting it's age when we create it.
Thinking in real-world terms, a person cannot skip ages - they cannot go from age 20 to age 30 instantly.
With the code we have here, we have the ability to set this person's age to any amount we want:
Code:
public class Main {
public static void main(String[] args) {
Person person = new Person(20);
person.age = 30;
}
}
We want to prevent this possibility. This is why we have encapsulation. Encapsulation is the act of restricting the access to an object's components. By making age private, we can safely continue programming knowing that age won't be modified in a way it's not supposed to from an outsider (someone using your class, but does not have access to the source).
But what if we want to check the age? Well, this is where "getter methods" come in.
Getter and setter methods are dedicated towards a specific private variable. One method that allows outsiders to re-assign the variable, and one method that allows outsiders to check the value stored in it.
Whats so great about getter/setters rather than using "public" is that you can choose to have only 1 if you want.
We only want outsiders to be able to check the value of age, not set it. In this case, we would make age private, and create a getter method for it like so:
Code:
class Player {
private int age;
public Player(int a) {
age = a;
}
public int getAge() {
return age;
}
}
Using the class above, we can create an instance of Player, setting it's age right when we create it. Once it's age is set, outsiders can no longer change it.
Let's say we do wanna let outsiders change it, but only if it's still logical. We could create a setter method which contains conditions to check if the re-assignment is valid:
Code:
public void setAge(int newAge) {
if(newAge < age)
System.out.println("You cannot age backwards!");
}
So as you can see, encapsulation gives the programmer more control. It allows you to set rules and ensure your program will and can only operate in the way you want it to.
This is VERY important to understand when working with other developers (or deploying source). It also keeps your code organized, so you are looking through every file, trying to see where you used a public variable.
Speaking of finding variable usage, getter/setters make this SO MUCH EASIER. I'm sure you can guess why; it allows you to see where you set and where you checked a variable, instead of just seeing where it was used. This makes it a lot easier to manage large applications.
This is just a basic guide to introduce some of you to the idea of it. I've been seeing a lot of released content lately, and it's great to know that people are doing independent applications. But if you release the source code, you gotta realize that you are the only one who is familiar with it. If you make everything public, public static, ect.. without using encapsulation, people will have a hard time reading through your code.
If you have any questions, or which for a more in-depth explanation, feel free to message me at any time