简体   繁体   中英

Refresher on Java classes in separate files

I need a refresher on moving classes from one file into two files. My sample code is in one file called " external_class_file_main ". The program runs fine and the code is shown below:

Public class external_class_file_main {

    public static int get_a_random_number (int min, int max) {
        int n;
        n = (int)(Math.random() * (max - min +1)) + min;
        return (n);

    }

    public static void main(String[] args) {
        int r;
        System.out.println("Program starting...");

        r = get_a_random_number (1, 5);
        System.out.println("random number = " + r);

        System.out.println("Program ending...");

    }

}

I move the get_a_random_number class to a separate file called " external_class_file ". When I do this, I get the following error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method get_a_random_number(int, int) is undefined for the type
external_class_file_main
at external_class_file_main.main(external_class_file_main.java:20)

The " external_class_file_main " now contains:

public class external_class_file_main {

    public static void main(String[] args) {
        int r;
        System.out.println("Program starting...");

        r = get_a_random_number (1, 5);
        System.out.println("random number = " + r);

        System.out.println("Program ending...");

    }

}

The " external_class_file " now contains:

public class external_class_file {

    public static int get_a_random_number (int min, int max) {
        int n;
        n = (int)(Math.random() * (max - min +1)) + min;
        return (n);

    }

}

You need to refer t get_a_random_number via the class external_class_file . Eg:

int r;
System.out.println("Program starting...");
r = external_class_file.get_a_random_number (1, 5);

You should definitely stick to Java naming conventions though.

You no longer have access to the get_a_random_number method from the external_class_file_main class. As the method you need is static you can just refer directly to it as follows:

public static void main(String[] args) {

    int r;
    System.out.println("Program starting...");

    r = external_class_file.get_a_random_number (1, 5);
    System.out.println("random number = " + r);

    System.out.println("Program ending...");

}

PS you will find it a lot easier to code and for people reading your questions if you use proper Java naming conventions for your methods and classes eg no underscores and start classes with a capital letter. http://en.wikipedia.org/wiki/Naming_convention_%28programming%29

Here the solution:

public class external_class_file_main {

    public static void main(String[] args) {
        int r;
        System.out.println("Program starting...");

        r = external_class_file.get_a_random_number (1, 5);
        System.out.println("random number = " + r);

        System.out.println("Program ending...");

    }

}

But, please, take a look into Java naming conventions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM