A
A
Albert Sultangulov2019-02-19 14:35:56
Java
Albert Sultangulov, 2019-02-19 14:35:56

I can't figure out how to use getters and setters?

I opened just one tab to study getters and setters, then I had to create a dozen more tabs in search of understandable information and explanations of a bunch of terms and commands from articles. Help me understand how getters and setters work.
Here for this example:

public class Cat {

   private String name;
   private int age;
   private int weight;

   public Cat(String name, int age, int weight) {
       this.name = name;
       this.age = age;
       this.weight = weight;
   }

   public Cat() {
   }

   public void sayMeow() {
       System.out.println("Мяу!");
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

Code from the JavaRush site. Please explain what get, set, this and return commands do by example or give your own example. Explanations like "returns a value" (where? Why? What does the returned value do?), "sets, assigns" (where?), "receives" (and what's next?) I can't understand. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-02-19
@Berht

It's better to read the tutorial than to open tabs. There will be more sense.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question