简体   繁体   中英

Can understand setters and getters in java

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");
    } 
}

You need them to allow users of your class to get and set parameters. This allows your class to keep track of when parameters are set and eg check constraints, enforce consistency, update other values, invalidate caches. The encapsulation also allows you to modify the internal representation of the class between versions while keeping the API compatible.

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.

A getter or setter is just a function that returns a class property, or sets a class property. The reason you use them is because typically class properties are defined as private , which means you can only reference them directly within class methods.

A getter is a public method (meaning it can be called from outside of the class itself) that returns a private value. This allows the property to remain private but still be accessible outside of the class.

A setter is a public method that sets a private value. As you may have guessed, this allows the property to remain private but still be set outside of the class.

Setters and getters are just methods that are used to access fields associated with a class:

class Demo {

    int a;

    public Demo () { a=4; }

    public int getDemo () { return a; }

    public void setDemo (int a) { this.a=a; }

}

The getters and setters are a way of accessing Java Bean properties associated with a class. It's just a standard means of being able to access the public fields that the class is interested in sharing. They are defined by the Java Bean specification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM