-1

I'm training on Android in the field of inheritance And I want to know why the definition of the name and color variables from the final keyword - And when I remove this keyword, no use is made. And when I get this keyword, there is no error or accident - Please tell me what the reason for using the final is

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView txtAnimal = (TextView) findViewById(R.id.txtAnimal);
    TextView txtCat = (TextView) findViewById(R.id.txtCat);

    Animal animal1 = new Animal("tiger", "orange", 60, 80);
    Cat cat1 = new Cat("persian", "brown", 40, 25, 4, true);

    txtAnimal.setText(animal1.toString());
    txtCat.setText(cat1.toString());


}

Animal.java

public class Animal extends Object{

    private final String name;
    private final String color;
    private int amountOfSpeed;
    private int amountOfPower;

    public Animal(String name, String color, int amountOfSpeed, int amountOfPower){

        // this. for same name
        this.name = name;
        this.color = color;
        this.amountOfSpeed = amountOfSpeed;
        this.amountOfPower = amountOfPower;
    }

    // we can use setter because variable (name-color) are defined final
    public String getName(){
        return name;
    }
    public String getColor(){
        return color;
    }
    public void setAmountOfSpeed(int amountOfSpeed){
        this.amountOfSpeed = amountOfSpeed;
    }
    public int getAmountOfSpeed(){
        return amountOfSpeed;
    }
    public void setAmountOfPower(int amountOfPower){
        this.amountOfPower = amountOfPower;
    }
    public int getAmountOfPower(){
        return amountOfPower;
    }

    public int evaluateAnimalValue(){
        int result = amountOfSpeed *amountOfPower;
        return result;
    }


    @Override
    public String toString() {
        return String.format("%s: %s  %s: %s  %s: %d  %s: %d",
                "Name", name,
                "Color", color,
                "Speed", amountOfSpeed,
                "Power", amountOfPower);
    }
}

Cat.java

  private final int numberOfLegs;
    private boolean canHuntOtherAnimal;

    public Cat(String name, String color, int amountOfSpeed, int amountOfPower, int numberOfLegs, boolean canHuntOtherAnimal){

        super(name, color, amountOfSpeed, amountOfPower);
        this.numberOfLegs = numberOfLegs;
        this.canHuntOtherAnimal = canHuntOtherAnimal;
    }



    public int getNumberOfLegs() {
        return numberOfLegs;
    }

    public boolean getCanHuntOtherAnimal() {
        return canHuntOtherAnimal;
    }

    public void setCanHuntOtherAnimal(boolean canHuntOtherAnimal) {
        this.canHuntOtherAnimal = canHuntOtherAnimal;
    }

    @Override
    public String toString() {

        return super.toString() + String.format("  %s: %d  %s: %b",
                "Legs", numberOfLegs,
                "Fight", canHuntOtherAnimal) + "  Animal Value: " + evaluateAnimalValue();
    }
}
Omid
  • 21
  • 1
  • 6
  • 1
    To **document** and **enforce** the fact that the value is immutable, i.e. cannot be changed after being assigned in the constructor. – Andreas Jan 18 '19 at 22:34
  • @Andreas If it is not final, how can change in the constructor? And any class that Inheritance can change? – Omid Jan 18 '19 at 22:38
  • @Andreas that's the best reason IMO. If you see something declared as final, you (usually) should be thinking to yourself "hey maybe I shouldn't be trying to edit this". Even though it's already impossible to edit from outside the class, declaring it as final gives you just a bit more safety while inside the class to prevent accidents. – Stalemate Of Tuning Jan 18 '19 at 22:39

1 Answers1

0

The final keyword for a variable means that the value cannot be modified. The variable must be set once, and then it cannot be changed.

The variable is initialized either when it is declared:

private final String name = "Rover";

...or in the constructor, as you have in your code above.

In your code sample above, a setName method would not work with a final name. However, you can call setAmountOfSpeed since amountOfSpeed is not final.

The final keyword is often used to represent constants:

public final float PI = 3.14159;

Here's an article for additional uses of the final keyword.

https://www.geeksforgeeks.org/final-keyword-java/

HeroSteve
  • 163
  • 1
  • 6
  • If the final can not be changed, why can it be changed here?Animal animal1 = new Animal("tiger", "orange", 60, 80); Cat cat1 = new Cat("persian", "brown", 40, 25, 4, true); – Omid Jan 18 '19 at 22:43
  • Does this mean that the variable names not change in the final? – Omid Jan 18 '19 at 22:48
  • In both cases, you are calling the constructor to assign the name the first time. The constructor is the only exception. To see for yourself, add this method to Animal: public void setName(String name) { this.name = name; } If you have a decent editor, it will show an error such as "The final field Animal.name cannot be assigned." – HeroSteve Jan 18 '19 at 23:00
  • ty Now I understand well – Omid Jan 19 '19 at 10:17