Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7705419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:51:40+00:00 2026-05-31T23:51:40+00:00

Iam a Java beginner and i would like to ask whats the pros and

  • 0

Iam a Java beginner and i would like to ask whats the pros and cons about this:

If i make a Class and i wont write my own setters and getters i can just get and set my class’s properties like:

myClassInstance.name = "Jones"
myClassInstance.job = "Manager"

System.out.println(myClassInstance.name);
System.out.println(myClassInstance.job);

Why better if i make getters and setters and do like this:

myClassInstance.setName("Jones");
myClassInstance.setJob("Manager");

System.out.println(myClassInstance.getName());
System.out.println(myClassInstance.getJob());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:51:41+00:00Added an answer on May 31, 2026 at 11:51 pm

    This question is related to one of the basic principals of OO design: Encapsulation!

    Accessors (also known as getters and setters) are methods that let you read and write the value of an instance variable of an object

    public class AccessorExample {
    private String attribute;
    
    public String getAttribute() {
     return attribute;
    }
    
    public void setAttribute(String attribute) {
     this.attribute = attribute;
    }
    }
    

    Why to use them?

    Getter and Setters make APIs more stable. Lets consider a field public in a class which is accessed by other classes. Now later on, you want to add any extra logic while getting and setting the variable. This will impact the existing client that uses the API. So any changes to this public field will require change to each class that refers it. On the contrary, with accessor methods, one can easily add some logic like cache some data, lazily initialize it later. Moreover, one can fire a property changed event if the new value is different from the previous value. All this will be seamless to the class that gets value using accessor method.

    Also Getters and setters methods allow different access levels – for eg. Get may be public, but the Set could be protected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am beginner with Java, and I would like to write some code like
I am a Java beginner and I would appreciate it if someone here can
I am just a beginner in Java and I would be glad if you
I am a beginner in software developement and I would like to know what
This is a continuous crawling for a lipster beginner on Clojure and Java. I
Am Beginner to java.I have a small doubt while i am developing an application
I am a total total beginner in Java actually I am .net dev trying
I am a beginner when it comes to programming with Android/Java, and I am
I am a complete JSP beginner. I am trying to use a java.util.List in
Firstly I am new to android and Java so this is a beginners question.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.