MOCKSTACKS
EN
Questions And Answers

More Tutorials









Java Access modifiers & getters setters in Java


Access Modifiers


Access Modifiers specify where a property/method is accessible. There are four types of access modifiers in java :

1. private
2. default
3. protected
4. public

class Employee {

    private int id;
    private  String name;

}

public  class CWH {
    public static void main(String[] args) {
        Employee emp1 = new Employee();
        emp1.id = 3;
        emp1.name = "Shubham";

    }
}

Output

java: id has private access in Employee

You can see that the above code produces an error that we're trying to access a private variable outside the class. So, is there any way by which we can access the private access modifiers outside the class? The answer is Yes! We can access the private access modifiers outside the class with the help of getters and setters.

Getters and Setters :


  • Getter ➼ Returns the value [accessors]
  • setter ➼ Sets / updates the value [mutators]


  • In the below code, we've created total 4 methods:

    1. setName(): The argument passed to this method is assigned to the private variable name.
    2. getName(): The method returns the value set by the setName() method.
    3. setId(): The integer argument passed to this method is assigned to the private variable id.
    4. getId): This method returns the value set by the setId() method.

    Examples


    class Employee {
    
        private int id;
        private  String name;
    
        public String getName(){
            return name;
        }
        public void setName(String n){
            name = n;
        }
        public void setId(int i){
            id = i;
        }
        public int getId(){
            return id;
        }
    }
    
    public  class CWH {
        public static void main(String[] args) {
            Employee emp1 = new Employee();
    
            emp1.setName("Shubham");
            System.out.println(emp1.getName());
            emp1.setId(1);
            System.out.println(emp1.getId());
    
        }
    }
    

    Output

    Shubham
    1

    As you can see that we've got our expected output. So, that's how we use the getters and setters method to get and set the values of private access modifiers outside the class.

    Conclusion

    In this page (written and validated by ) you learned about Java Access modifiers, getters & setters in Java . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java Constructors.



    Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


    Share On:


    Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.