【问题标题】:Why can I not instantiate a new Class object?为什么我不能实例化一个新的 Class 对象?
【发布时间】:2021-10-15 04:12:09
【问题描述】:

所以这是一个真正令人头疼的问题:我有一些代码可以测试要满足的某些条件;当遇到时创建一个新对象,然后为该对象设置一些值,然后将该对象添加到 PriorityQueue 以供以后进一步处理。代码如下所示:

...
import com.example.Position; //Class is properly imported here
...

@Service
public class TradeService extends Thread {
    private final Agent uAgentAcct = new Agent();
    private final HttpClient httpClient = HttpClient.newHttpClient();
    private final Clock clock = new Clock(httpClient);
    private final Authenticator auth = new Authenticator(clock.getServerTime());
    private final MessageConverterFactory parseMsg = new MessageConverterFactory();
    public static ArrayList<Asset> watchList = new ArrayList<Asset>();
    private Position activePosition;
    
    @Override
    public void run() {
        [redacted]
    }
    
    private static Position entryScan(ArrayList<Asset> tips) {
        PriorityQueue<Position> posQ = new PriorityQueue<Position>(new PositionComparator());
        for(int i = 0; i < tips.size(); i++) {
            Asset token = tips.get(i); //Instantiating a new Asset instance works just fine right here...
            ArrayList<Candle> chart = token.getChart();
            if(chart.size() >= 14) {
                [redacted]
                if(downTrend && reversion && rsi < 10 && current.getClose() > last.getHigh()) {
                     Position long = new Position(); //But I can't instantiate a new Position instance here? This gets an error from the compiler that states: Position cannot be resolved to a variable
                }
            }
        }

如上所述,我有一个名为“Position”的类被导入到这个类中;但是,当尝试在我的私有静态 entryScan 方法的 if 块中创建此类的新实例时,编译器会表现得好像它不知道我正在引用什么类,这对我来说完全没有意义。我给出的唯一快速修复选项(我正在使用 Spring Boot,顺便说一句)是创建一个字段“Position”,创建一个局部变量“Position”,或创建一个参数“Position”;我不想或不需要做的任何事情。通常我希望有一个导入类的选项;但由于它已经被导入,我不确定为什么编译器无法识别它。就好像它试图将我的变量类型声明读取为 TradeService 的字段值,这与我实际引用的内容相去甚远。所以我想我的问题是为什么它会这样表现,我如何强制它“看到” Position 类作为一个类,而不是一个字段值?

【问题讨论】:

  • long是Java中的保留字,指的是64位整数类型

标签: java spring-boot class variable-declaration


【解决方案1】:

您正在为变量名使用保留关键字long。将其更改为其他内容,例如Position position = new Position();

【讨论】:

  • 当然,我怎么没听懂!非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-07
  • 2014-03-08
  • 1970-01-01
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多