【问题标题】:Cannot find symbol... - Java [duplicate]找不到符号... - Java [重复]
【发布时间】:2015-12-15 03:18:11
【问题描述】:

我是 OOP 概念的新手,我收到一个错误:找不到符号。同样在编译期间,我得到 2 个错误:

Error:(9, 35) java: <identifier> expected
Error:(9, 36) java: illegal start of type

任何帮助将不胜感激。 这是我的两个课程:

package com.company;

public class Main {

    public static void main(String[] args) {

    }

    TestClass waterBottle = new waterBottle();
    waterBottle.bottleFill(5);
}

package com.company;

public class TestClass {

    TestClass() { }

    public void waterBottleFill(int y) {
        int bottleFill = y;
        System.out.println("Fill level is at:" + bottleFill);
    }

    public void waterBottleRefill(int x) {
        int refill = x;
    }
}

【问题讨论】:

  • 你的文件名是什么?你的系统中是否安装了java。我相信您在 CLI 中遇到错误。
  • 两个类都在不同的java文件中?

标签: java oop syntax-error symbols


【解决方案1】:

你的代码

TestClass waterBottle = new TestClass();
waterBottle.bottleFill(5);

实际上位于类定义中。

将此代码移至您的 main 方法:

public class Main {

    public static void main(String[] args) {
        TestClass waterBottle = new waterBottle();
        waterBottle.bottleFill(5);
    }
}

当您正确格式化您的代码时,这个问题会变得很明显 - 使用标识、垂直和水平间距。我已经编辑了你的问题,现在它的格式正确。看看它现在的可读性。

另一个问题是您的类对象创建。您在new waterBottle() 行提供了不正确的名称。 new 后面应该跟一个正确的类名:

TestClass waterBottle = new TestClass();

【讨论】:

    【解决方案2】:
    package com.company;
    
    public class Main {
    
    public static void main(String[] args) {
    
    
        //TestClass waterBottle = new waterBottle(); // what is waterbottle.. this will give you can not find symbol`
        TestClass waterBottle = new TestClass();//should be this
    
    
            waterBottle.bottleFill(5);
    }// main method ends
    
    }// class ends
    
    
    
    
    package com.company;
    
    public class TestClass {
    
    TestClass(){}
    
    public void waterBottleFill(int y){
        int bottleFill = y;
        System.out.println("Fill level is at:"+ bottleFill);
    
    }
    public void waterBottleRefill(int x){
        int refill = x;
    }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-09
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 2018-05-13
      • 2010-10-12
      相关资源
      最近更新 更多