kbluue
kbluue

Reputation: 379

How to create/use/import a custom made Method on Java

Like the in-built Math class, there are a couple of methods that one can use without importing the Math class. e.g

 int io = (int) Math.random();

and notice the import region: no MATH whatsoever

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

but seeing, Math set doesn't have everything i needed, i created mine in a new class, but i can't seem to figure out what to do so i can be able to use it.

Taking a hint from the Math.java file, I've made my class final and my methods static but no avail..

Here's an excerpt of my code

package customops.Sets;

/** * * @author Kbluue */

public final class SetOpz {

public SetOpz(){}

public static int setMax(int[] set){
    int out = set[0];
    for(int i=1; i<set.length; i++){
        out = Math.max(out, set[i]);
    }
    return out;
}

how do i use just the import command without having to copy and paste the SetOpz class in the DTL package?

how do i use just the import command without having to copy and paste the SetOpz class in the DTL package?

Upvotes: 0

Views: 1608

Answers (2)

Shubham Kharde
Shubham Kharde

Reputation: 228

You can import your method wherever you want to use with the following import statement

import static customops.Sets.SetOpz.setMax;

Upvotes: 0

Zarwan
Zarwan

Reputation: 5797

You don't need to import Math explicitly because it is included by default. To use your own code you will have to import it. If you're using IntelliJ or Eclipse or some other smart IDE it will offer to import it for you automatically. Otherwise add a import statement at the top:

import customops.Sets.SetOpz;

Upvotes: 1

Related Questions