how to import a self written class in java

‮al.www‬utturi.com
how to import a self written class in java

To use a class that you have written in your Java code, you need to import the class into the file where you want to use it.

To import a class, you can use the import keyword followed by the fully qualified name of the class. For example, if you have a class called MyClass in a package called com.example, you can import the class with the following statement:

import com.example.MyClass;

You can then use the class in your code by creating an instance of the class and calling its methods.

MyClass myObject = new MyClass();
myObject.someMethod();

If the class is in the same package as the file where you are using it, you don't need to use the import statement. You can simply use the class name without the package prefix.

MyClass myObject = new MyClass();
myObject.someMethod();

Note that you can also use the import static statement to import static members (fields and methods) of a class. This allows you to use the static members without specifying the class name.

import static com.example.MyClass.someStaticMethod;

// Call the static method without specifying the class name
someStaticMethod();
Created Time:2017-11-01 20:42:56  Author:lautturi