I’m learning Java in VSC and found odd issue when playing around with Queues
.
Below code throws cannot be resolved to a type
error when trying to use static
variable.
Java I’m using:
java 15 2020-09-15
Java(TM) SE Runtime Environment (build 15+36-1562)
Java HotSpot(TM) 64-Bit Server VM (build 15+36-1562, mixed mode, sharing)
Thanks for help!
import java.util.LinkedList;
import java.util.Queue;
public class theQueue {
public static void main(String[] args) {
Queue<recordForHuman> market = new LinkedList<>();
market.add(new recordForHuman("Alex", 21));
}
//'record' is not a valid type name; it is a restricted identifier and not allowed as a type identifier in Java 15
//static record Human(String name, int age){}
//recordForHuman cannot be resolved to a type
static recordForHuman Human(String name, int age){}
}
2
Answers
That is not a static variable deceleration. It seems to define a static method called
Human
that returns arecordForHuman
. There is no declaration of a class or interfacerecordForHuman
in the code shared so we don’t know what that is. Note that by Java naming convention a type should start with an uppercase letter and a method with a lower case letter.record
is a reserved word since Java 14 so you can not use it for any identifier.To fix the error create a type with a proper name and use it.
If the static Human() is supposed to be a variable, then you need to make an instance like you did with the queue,
Human h = new Human()
, if it’s supposed to be a method, then you need to either create arecordForHuman
class for the method to return.