The left-hand side of an assignment must be a variable error appears.

Asked 2 years ago, Updated 2 years ago, 38 views

Why do I get errors in the following programs?

▪ Test 85.java

import java.util.function.*;

public class Test 85 {
    public static void main(String[]args) {
//      Hero85h = new Hero85();
        (Cat85c) - > {
            return c.getHp();
            }
    }
}

▪ CCat85.java

public interface Cat85 {
    public inthp;

    Cat85(){
        This.hp = 100;
    }

    public abstract int getHp(){
        return this.hp;
    }

}

▪ EError Log

The left-hand side of an assignment must be a variable
Multiple markers at this line
    - Syntax error, insert "AssignmentOperator Expression"
     complete Expression
    - Syntax error, insert"; "to complete BlockStatements"

Please reply

java

2022-09-30 19:43

1 Answers

Cat85 is declared on the interface.Therefore, the following compilation errors may appear:

  • Interface cannot have constructors

Also, getHp() declared by the abstract method is an abstract method, so

  • Abstract methods cannot have body

and compilation errors.

I think I should make Cat85 a normal Java class, so for example,

public class Cat85 {
    public inthp;

    Cat85(){
        This.hp = 100;
    }

    public int getHp(){
        return this.hp;
    }
}

As , the variable hp contains 100 when the Cat85 instance materializes.

Perhaps what you want to implement in the Test85 class is to get an abstracted Cat85 instance and get its hp value, so this is a simple example, but if you do this, you get the Cat85 instance using the streamAPI feature available in java8 and return the value of the hp in it:

public class Test 85 {
    public static void main(String[]args) {

        Supplier<Cat85>supplier=()->new Cat85();
        int hp = supplier.get().getHp();

        System.out.println(hp);
    }
}

Perhaps the class provided by the supplier wanted to return something with Cat85 implementation of the interface.


2022-09-30 19:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.