Results 1 to 7 of 7
Thread: Writing importable classes
- 03-08-2009, 07:20 PM #1
Writing importable classes
In Java the word "import" is used in order to... well, import other classes (or aren't they classes?), such as swing, util, awt, and so forth. My question is: is it possible to write my own class, with functions/objects of its own, and then use "import" in order to use that class's qualities when writing different codes? And if so, how do I do it? (Do I need to save the file in java's home directory, does it have to contain specific modifiers like "abstract", etc.) A general explanation would be very helpful to me.
Thanks in advance, and I apologize if this is a frequently asked question - my googling on the subject came up pretty empty.
-
Any class that you create that has a proper package structure is theoretically importable. So, in other words, you've likely created classes that can already be imported by other Java classes found in different packages (no need to import if in the same package). A caveat though is that the class file that you want to import must be in the classpath.
- 03-08-2009, 07:24 PM #3
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
import packages. classes are in packages.
- 03-08-2009, 07:39 PM #4
I'm not familiar with packages. How do I use them in order to create my own importable functions, objects etc.?
-
You can find out about packages at the Sun Java tutorials here:
Lesson: Packages (The Java™ Tutorials > Learning the Java Language)
Best of luck!
- 03-08-2009, 09:04 PM #6
I'm not quite sure how to use that information for creating importable packages (thanks for helping btw).
Let's say I have the following code:
1. Does the code contain mistakes?Java Code:// MyFunctions.java package My_Functions; public class MyFunctions { static int[] copyArr(int[] arr) { int[] clone = new int[arr.length]; for(int i = 0; i < arr.length; i++) { clone[i] = arr[i]; } return clone; } }
2. Assuming it does not - how can I import My_Functions from a completely different file, and use the copyArr function?
-
It's not a good idea to use forum members as a surrogate Java compiler. Better would be to try to compile, run and test the code first yourself, then come back with questions if you find a mistake.1. Does the code contain mistakes?
Since it's a static method, I'd just call it with a "fully qualified" name just like you use the println(...) method:2. Assuming it does not - how can I import My_Functions from a completely different file, and use the copyArr function?
This should work again as long as the class file for this class is on the class path when you compile the new class, the class that uses this method. I'm guessing that you'll want to go to the Sun tutorials and read up on classpath. Best of luck.Java Code:int[] myArray = My_Functions.MyFunctions.copyArr(myOtherArray);
Similar Threads
-
writing to gui
By rob in forum New To JavaReplies: 2Last Post: 02-13-2009, 10:55 PM -
Making A Set Of Classes "Importable"
By JDCAce in forum Advanced JavaReplies: 4Last Post: 12-05-2008, 09:11 AM -
Writing classes in graphics
By CyberFrog in forum Java AppletsReplies: 2Last Post: 04-05-2008, 05:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks