Results 1 to 7 of 7
  1. #1
    Katsu's Avatar
    Katsu is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    import packages. classes are in packages.

  4. #4
    Katsu's Avatar
    Katsu is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    I'm not familiar with packages. How do I use them in order to create my own importable functions, objects etc.?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You can find out about packages at the Sun Java tutorials here:
    Lesson: Packages (The Java™ Tutorials > Learning the Java Language)

    Best of luck!

  6. #6
    Katsu's Avatar
    Katsu is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    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:

    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;
    	}
    }
    1. Does the code contain mistakes?
    2. Assuming it does not - how can I import My_Functions from a completely different file, and use the copyArr function?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    1. Does the code contain mistakes?
    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.
    2. Assuming it does not - how can I import My_Functions from a completely different file, and use the copyArr function?
    Since it's a static method, I'd just call it with a "fully qualified" name just like you use the println(...) method:

    Java Code:
    int[] myArray = My_Functions.MyFunctions.copyArr(myOtherArray);
    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.

Similar Threads

  1. writing to gui
    By rob in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 11:55 PM
  2. Making A Set Of Classes "Importable"
    By JDCAce in forum Advanced Java
    Replies: 4
    Last Post: 12-05-2008, 10:11 AM
  3. Writing classes in graphics
    By CyberFrog in forum Java Applets
    Replies: 2
    Last Post: 04-05-2008, 06:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •