can not find the class in separate files

advertisements

This is my first time doing java codes, and I am confused on this. I supposed to compile 2 files, the first is Lingkaran.java which contains class lingkaran, and the second is MLingkaran.java which is the main file. The Lingkaran.java compiles just fine, but when I tried to compile the MLingkaran.java, these errors came out.

C:\Users\LENOVO PC>javac F:\SchoolSums-Praktikum\Semester4\PemrogramanBerbasisObjek\14.03.17\2\MLingkaran.java F:\SchoolSums-Praktikum\Semester4\PemrogramanBerbasisObjek\14.03.17\2\MLingkaran.java:11: error: cannot find symbol lingkaran l = new lingkaran(); ^ symbol: class lingkaran location: class mLingkaran F:\SchoolSums-Praktikum\Semester4\PemrogramanBerbasisObjek\14.03.17\2\MLingkaran.java:11: error: cannot find symbol lingkaran l = new lingkaran(); ^ symbol: class lingkaran location: class mLingkaran 2 errors

The following are the source code:

Lingkaran.java:

class lingkaran {
    private double jari;
    private double luas;
    private double keliling;
    private double phi;

    public lingkaran(){
        this.phi = 3.14;
        this.jari = 10;
        this.keliling =  this.jari*2*this.phi;
        this.luas = this.phi * this.jari * this.jari ;
    }

    public double getLuas() {
        return this.luas;
    }

    public double getKeliling() {
        return this.keliling;
    }
}

and the MLingkaran.java:

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

        lingkaran l = new lingkaran();

        System.out.println("keliling = " +l.getLuas());
        System.out.println("luas = " +l.getKeliling());
    }
}


You need to import the other files to be able to use anything in them.

import <package>.Lingkaran;

If the file isn't in a package (it should be though), then you can leave the first part out.

Let's take the following project structure:

package1
    MLingkaran
    package2
        Lingkaran

Then in MLingkaran you need to use

import package1.package2.Lingkaran;