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 6998137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:22:22+00:00 2026-05-27T20:22:22+00:00

I’m new to java, but I like the language and I want to become

  • 0

I’m new to java, but I like the language and I want to become as good as possible but I still have some difficulties with the “class” thing.
I understand what a class is and what objects are but still I cant understand well getters and setters

I know a class has

  • Instance variables
  • Constructors
  • Methods
  • Setters and getters ?

I can’t understand well why do I need them and if I should write those inside a constructor.

Also like to point out that I’ve read several internet articles but

  1. those were to much “technical”
  2. they just write a code without instance variables, constructors so I can’t understand very well

Your help is very appreciated even if you are a beginner like me ^^

Edit:

For example how i should use the setters and getters here?

class Demo {

    int a=4;
    Int b;

    public Demo () {
        System.println("a is <than b")
    };

    public int Demo (int b) { 
        if (a<b)
            System.out.println("a is < than b");
        else
            System.out.println("b is < than a");
    } 
}
  • 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-27T20:22:23+00:00Added an answer on May 27, 2026 at 8:22 pm

    You asked for a simple example, so I’ll give one. Suppose you’re designing a circle class. A circle can be characterized by its diameter:

    public class Circle {
        private double diameter;
    
        public Circle(double diameter) {
            this.diameter = diameter;
        }
    }
    

    A caller might want to know the diameter of the circle, so you add a getter:

    public class Circle {
        private double diameter;
    
        public Circle(double diameter) {
            this.diameter = diameter;
        }
    
        public double getDiameter() {
            return this.diameter;
        }
    }
    

    You might also want to get the area of the circle, so you add a getter for the area:

    public double getArea() {
        return Math.PI * (this.diameter / 2) * (this.diameter / 2);
    }
    

    And then you realize that using the radius rather than the diameter is easier for the internal methods of the circle. But you want to keep all the users of your class as is, to avoid breaking a lot of existing code. So you change the internals of the class without changing the interface:

    public class Circle {
        private double radius;
    
        public Circle(double diameter) {
            this.radius = diameter / 2;
        }
    
        public double getArea() {
            return Math. PI * radius * radius;
        }
    
        public double getDiameter() {
            return this.radius * 2;
        }
    }
    

    And finally, you would like to change the diameter of the circle, so you add a setter:

    public void setDiameter(double diameter) {
        this.radius = diameter / 2;
    }
    

    But wait, a circle with a negative diameter makes no sense, so you make the method safer:

    public void setDiameter(double diameter) {
        if (diameter <= 0) {
            throw new IllegalArgumentException("diameter must be > 0");
        }
        this.radius = diameter / 2;
    }
    

    Had you precomputed the area at construction time, the setDiameter would have had to change the value of the area as well:

    public class Circle {
        private double radius;
        private double area;
    
        public Circle(double diameter) {
            this.radius = diameter / 2;
            this.area = Math. PI * radius * radius;
        }
    
        public double getArea() {
            return area;
        }
    
        public double getDiameter() {
            return this.radius / 2;
        }
    
        public void setDiameter(double diameter) {
            this.radius = diameter / 2;
            // the area must also be changed, else the invariants are broken.
            this.area = Math. PI * radius * radius;
        }
    }
    

    Getters and setters are just methods. But they follow a naming conventions that makes your code easy to grasp, and usable by a number of frameworks which rely on these conventions.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I have a text area in my form which accepts all possible characters from
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to

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.