how to separate code in eclipse?

0

Suppose I have main.java and code.java

in the ide processing you can open a tab and write a function and then use it in draw ()

try doing something like this in eclipse

in main.java I have

public class main {

    public static void main(String[] args) {
        // Prints "Hello, World" to the terminal window.
        print_hello();
    }

}

in code.java I have

void print_hello()
{

        System.out.println("Hello, World");
}

but it throws me errors

as I open an empty tab to continue programming but that pertenesca to main.java

I want to do this for programs of many code lines

    
asked by jony alton 31.07.2018 в 01:28
source

2 answers

1

You can use the static modifier in the print_hello method, since it belongs to the class, and not to an object:

public class Code {

    public static void print_hello() {
        System.out.println("Hello, world");
    }

}
public class Main {

    public static void main(String[] args) {
        Code.print_hello();

    }

}

Exit:

  

hello, world

    
answered by 31.07.2018 в 08:19
0

You can use the static methods and so you avoid instantiating objects but remember java is object oriented so if you plan to write a lot of code it would be good to use this paradigm and leave the static methods to very specific tasks.

An example is the class Math that exemplifies a class which supports a certain number of subroutines or methods which do not need to be instantiated in order to be used.

Greetings ....

    
answered by 01.08.2018 в 01:06