Home > front end >  Cannot access class in the Same package in Java?
Cannot access class in the Same package in Java?

Time:08-12

I am currently working on a Gradle build for a java project but am struggling to figure a couple things out. I resumed to JAVA after years back so if this is a stupid question please help me fellow developers.

package dummmy.app;

import java.util.ArrayList;
import java.util.Arrays;

public class Cargo{
    String name;
    public Cargo(String name){
        this.name = name;
    }

    public String getCargo(){
        return(this.name);
    }
}

I am trying to implement this class in another class called Station and this is how the Station class looks like.

package dummmy.app;
import java.util.List;
import dummmy.app.Cargo;
public class Station{
    String name;
    ArrayList<Cargo> cargo;

    public Station(String name, ArrayList<Cargo> cargo){
        this.name = name;
        this.cargo = cargo;
    }

    public void getCargo(){
      System.out.print(cargo);
    }

    public static void main() {
      
    }
    
}

I cannot understand why during compilation, this error shows up?

Station.java:6: error: cannot find symbol
    ArrayList<Cargo> cargo;
    ^
  symbol:   class ArrayList
  location: class Station
Station.java:6: error: cannot find symbol
    ArrayList<Cargo> cargo;
              ^
  symbol:   class Cargo
  location: class Station
Station.java:8: error: cannot find symbol
    public Station(String name, ArrayList<Cargo> cargo){
                                ^
  symbol:   class ArrayList
  location: class Station
Station.java:8: error: cannot find symbol
    public Station(String name, ArrayList<Cargo> cargo){
                                          ^
  symbol:   class Cargo
  location: class Station
4 errors

CodePudding user response:

Two problems in your code.

You've imported java.util.List, however, you have failed to import java.util.ArrayList.

Also, your Cargo class has no package statement, whereas your Station class does, which trivially means they aren't in the same package.

  • Related