Import a custom class in Java

前端 未结 7 1467
悲哀的现实
悲哀的现实 2020-11-29 06:57

How do I import a class I wrote in a different file? All my classes are under the same package.

7条回答
  •  难免孤独
    2020-11-29 07:30

    In the same package you don't need to import the class.

    Otherwise, it is very easy. In Eclipse or NetBeans just write the class you want to use and press on Ctrl + Space. The IDE will automatically import the class.

    General information:

    You can import a class with import keyword after package information:

    Example:

    package your_package;
    
    
    import anotherpackage.anotherclass;
    
    public class Your_Class {
        ...
        private Vector variable;
        ...
    }
    

    You can instance the class with:

    Anotherclass foo = new Anotherclass();
    

提交回复
热议问题