Search code examples
javainclude

Include Java files like PHP's include


Is there a way to import a .java file in another .java file like PHP's include? When using PHP, it imports the entire included page, including functions, variables etc. So basically it should like copy-paste the other file's source in the main file.
I have a pretty big Java file right now, and it's getting kinda unorganized.


Solution

  • Yep! It's called import.

    It will look something like this:

    package com.somename.someabstraction;
    
    import com.somename.someabstraction.somepackage;
    
    
    public class MyClass {
    
    //...
    

    You will not be able to mimic the include function of PHP because Java is not served "on-the-fly" like PHP, JSP or JS.

    Further, Java code by convention is broken down into more meaningful abstractions through the use of classes, packages and import statements.