D
D
Dmitry Popov2018-11-12 17:16:02
Java
Dmitry Popov, 2018-11-12 17:16:02

Why do we need methods that return the value of a class field?

For example:

class Person{
    private String name;
    private int age;
    public Person(String n, int a){       
        name=n;
        age=a;
    }
    String getName(){return name;}
    int getAge(){return age;}
}

I often see that for each field a method is set that returns a value.
Why do they do it this way? After all, you can get this value by writing objName.atrName

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question