0

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?

kbluue
  • 369
  • 1
  • 5
  • 20
  • The `Math` class is in the `java.lang` package, which is implicitly imported. You would need to import `customsops.Sets.SetOpz` in whichever file you wanna use it in. You do not need to make the method `static` to make it work. Although, you would need to create an instance of the class to access the method if it weren't `static` – Vince Jul 19 '15 at 18:37

2 Answers2

1

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;
Zarwan
  • 5,537
  • 4
  • 30
  • 48
  • I ttried all imports but none seem to work. But i created a new class in the same package folder and then it was recognizable. Isnt it possible that i use the SetOpz class without having to duplicate it in a new class and moving it into the same package i want to use it in??? I thought that that was the function of import. – kbluue Jul 19 '15 at 20:44
  • It should work even if it is in a different package as long as you provide the fully qualified path. See this: http://stackoverflow.com/questions/3480389/java-how-to-use-classes-in-other-package – Zarwan Jul 19 '15 at 20:48
  • I read the piece but i didnt entirely understand it. Could ypu please help me generate an import path with the following info. package customops.Sets; filepath C:\Users\Kbluue\Documents\NetBeansProjects\CustomOps\src\customops\Sets\SetOpz.java; i've tried adding com. but the only recognizable options are 'oracle' and 'sun' and when i define the path com.customOps.Sets.SetOpz myslf, it is not recognized by Netbeans so it refuses to run. Also when i add a new package, it changes the folder of the class entirely. – kbluue Jul 19 '15 at 21:38
  • I would need to see your entire project structure, or at least the root that the two packages share. – Zarwan Jul 19 '15 at 21:40
  • check picture above, thats it, just 2 different projects and they are both stored in the same Documents folder on my HDD – kbluue Jul 20 '15 at 00:20
  • Oh, if they're in different projects you will have to add it as a library in your new project or add it to your build path. – Zarwan Jul 20 '15 at 00:23
  • Please could you show me how to do that? @Zar Thanks i figured it out. Very Grateful :) pls could you also help with this: >>> http://stackoverflow.com/questions/31507380/how-to-create-a-method-that-can-take-in-multiple-classes-as-input – kbluue Jul 20 '15 at 00:29
  • @kbluue if your question is answered please mark one of the answers as correct with the green checkmark thing. I'll read your other question. – Zarwan Jul 20 '15 at 00:40
0

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

import static customops.Sets.SetOpz.setMax;
Shubham Kharde
  • 228
  • 2
  • 10