简体   繁体   English

当我从另一个类调用对象时出现错误,即“找不到符号”

[英]Error comes when i call objects from another class i.e. “cannot find symbol”

my program gives error when i try to compile it. 当我尝试编译它时,我的程序给出了错误。 I don't know whats wrong. 我不知道怎么了 Please help me. 请帮我。 It gives error in 21st and 22nd line. 它在21和22行给出错误。 Code is 代码是

class Rectangle {

    int length;
    int breadth;

    void setdata(int x, int y) {
        length = x;
        breadth = y;
    }

    int Area() {
        int rectarea = length * breadth;
        return rectarea;
    }
};

class Calculate {

    public static void main(String args[]) {
        Cal obj = new Cal();
        obj.setdata(3, 4);
        int result = obj.Area();
        System.out.println("Result is " + result);

    }
};

It gives error in obj.setdata(3,4); 它在obj.setdata(3,4);给出错误obj.setdata(3,4); and int result=obj.Area(); int result=obj.Area(); . Please help me and tell me where i am doing wrong. 请帮助我,告诉我我在哪里做错了。

The name of your class is Rectangle and not Cal so replace this piece : 您的班级名称是Rectangle而不是Cal因此,请替换此段:

Cal obj=new Cal();

By: 通过:

Rectangle obj = new Rectangle(); 

Note 注意

It is not require to set ; 不需要设置; after } like you do in the end of your classes }; }之后,就像在课程结束时一样}; .

Hope this can help you 希望这可以帮到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用另一个文件中的 class 时,我不断收到一条错误消息,提示“找不到符号” - I keep getting an error that say “cannot find symbol” when using a class from another file 错误:找不到符号[编译错误]无法调用另一个类 - error: cannot find symbol[Compile Error] Unable to Call Another class JAVA:为什么继续出现错误:尝试访问另一个类文件时找不到符号? - JAVA: Why do I keep getting the error: cannot find symbol when I try to access another class file? Java找不到符号错误-来自另一个类的方法 - Java cannot find symbol error - a method from another class 从另一个类调用方法时,Java 找不到符号 - Java cannot find symbol when calling a method from another class 尝试关闭I / O对象时“找不到符号” - “cannot find symbol” when trying to close I/O objects 如何直接从另一个类访问变量(即不使用类名或对象)? - How to access variable from another class directly(i.e. without using class name or object)? 尝试创建此类的实例时,为什么会出现“找不到符号”错误? - Why am I getting a “cannot find symbol” error when I try to create an instance of this class? 错误:找不到符号-当它依赖于子文件夹中的类时,如何使我的类编译? - ERROR: Cannot find symbol — How do I get my class to compile when it depends on a class in a subfolder? 当我返回一个数组时,我得到一个“找不到符号”的错误 - when I return an array, I get a “cannot find symbol” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM