Thread: Using private variables with getter/setters (encapsulation)

Results 1 to 4 of 4
  1. #1 Using private variables with getter/setters (encapsulation) 
    Registered Member
    Join Date
    May 2014
    Posts
    85
    Thanks given
    30
    Thanks received
    14
    Rep Power
    20
    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
    Reply With Quote  
     

  2. Thankful user:


  3. #2  


    Major's Avatar
    Join Date
    Jan 2011
    Posts
    2,997
    Thanks given
    1,293
    Thanks received
    3,556
    Rep Power
    5000
    There are lots of other advantages to using accessors & mutators too - if you want to count the amount of times a field is modified/accessed (useful for debugging), or if you want to implement a listener system that notifies whenever the field is changed (useful for any event-driven system) etc. Also OP, please use the default text size (2).
    Reply With Quote  
     

  4. #3  
    Registered Member
    Join Date
    May 2014
    Posts
    85
    Thanks given
    30
    Thanks received
    14
    Rep Power
    20
    Quote Originally Posted by Major View Post
    Also OP, please use the default text size (2).
    Fixed up

    Quote Originally Posted by Major View Post
    if you want to implement a listener system that notifies whenever the field is changed
    Great information! But the target audience most likely wont understand this. I'm hoping that people who knowingly use event-driven systems already understand the basics of encapsulation (what this topic introduces); I posted this due to the applications I've seen posted on here with source.
    Reply With Quote  
     

  5. #4  
    Registered Member
    Join Date
    Oct 2013
    Posts
    189
    Thanks given
    80
    Thanks received
    37
    Rep Power
    12
    Thank you for this tutorial, great contribution for the beginners community.

    I always wondered why I should make the variable private and use getters/setters instead when I was beginner so I never used them. But luckily I use them now because I understand the purpose of it.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 163
    Last Post: 03-20-2010, 05:02 PM
  2. Getters & Setters - Why you should use them
    By PSNB in forum Tutorials
    Replies: 11
    Last Post: 12-15-2009, 01:51 PM
  3. Replies: 31
    Last Post: 10-17-2009, 08:51 PM
  4. 2 fun commands i use to mess with players lol..
    By Runebay™ in forum Configuration
    Replies: 9
    Last Post: 05-15-2009, 05:54 AM
  5. Run Private Server with Linux?
    By thedoom in forum Software
    Replies: 6
    Last Post: 02-20-2009, 04:27 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •