Creating two classes in one file?

Hello, I am new to this forum and I am confused on how to make two classes in one java file. I made a class called point to represent a point with direction for problem 3 on the December Bronze but when I put it into the grader, an error came out.
stuckrut.java:9: error: cannot find symbol
point[] points = new point[N];
^
symbol: class point
location: class stuckrut

3 errors
My code was formatted like this (broad version):
public class stuckrut{
public static void main(String[]args){
//code
}
class point{
//code
}
}
Is there any way to have multiple classes in one java file? Let me know if I need to clarify anything.

Can you please include the full code? This code works for my computer without errors:

public class stuckrut {
    public static void main(String[] args) {
        int N = 100000;
        point[] points = new point[N];
    }

    class point {
    }
}

Oh, did you actually nest class point into class stuckrut or are they separate files?

You have basically two different options for multiple classes in one file.

You could create a static class within Stuckrut (it doesn’t matter that much in the competitive programming context, but conventionally in Java, names of classes start with an uppercase) so that the main method can use it as well as other (static) parts of Stuckrut, like so:

public class Stuckrut {
    static Point p1;

    public static void main(String[] args) {
        Point p2 = new Point(1, 2);
    }

    static void foo() {
        Point p3;
    }

    static class Point {
        int x, y;
        Point(int a, int b) {
            x = a;
            y = b;
        }
    }
}

You could alternatively create an entirely new non-static new class outside of Stuckrut. For this option, any other class, including Stuckrut, can access Point.

public class StuckRut {
    public static void main(String[] args) {
        Point p = new Point(1, 2);
    }
}

class Point {
    int x, y;
    Point(int a, int b) {
        x = a;
        y = b;
    }
}

class OtherClass {
    Point p;
}
1 Like

Ok I understand. Thanks so much for the response :+1: