I faced with this error: cannot find symbol

Viewed 49
class Example {

    public static void main(String[] args) {

        System.out.println("Hello World!");
        System.out.println(Sample.a);   
    }
}


class Sample{
   
     static int a = 10;
    public static void main(String[] args) {

        System.out.println("Hello");
        System.out.println(a);
    
    }
}

I faced with error:

cannot find symbol
System.out.println(Sample.a);
symbol: variable Sample
location: class Example

So that how to solve this error?

1 Answers

create 2 separate files. Each for 1 class. It is neat that way. Make them in the same directory so you don't need to import anything.

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

        System.out.println("Hello World!");
        System.out.println(Sample.a);
    }
}

then in class Sample, make 'a' public

public class Sample {
    public static int a = 10;
    public static void main(String[] args) {

        System.out.println("Hello");
        System.out.println(a);

    }
}
Related