Results 1 to 10 of 10
  1. #1
    kizzie33 is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default Can i including methods into a class ?

    Is there a way i can include methods into a class, i dont mean using the extends but just have all the methods from a file/class be put into a other classes as if i typed them in there my self

    Reason - I have a class called tools, it has several functions which do nothing amazing, they just life easier such as
    public void print(text){ System.out.println(text); }

    It doesn't need any special formatting, just needs to be included in all of my classes.

    Thankyou !

  2. #2
    demonick is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    You can include the files you need in the classpath and write the correct import statement in the class where they are needed, I think that's what you want.

  3. #3
    kizzie33 is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by demonick View Post
    You can include the files you need in the classpath and write the correct import statement in the class where they are needed, I think that's what you want.
    Ermm, how would i do that :S, is there a tutorial or instructions or something like that, i use eclipse if that has anything to do with how i change the classpath

  4. #4
    demonick is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    Go to File -> Project properties and find where you can import files, can't tell exactly now, after you import them they will be on the classpath.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Is this Tools class part of your project?

    If so then you should be able to just do an import in the class you want to use these methods, assuming Tools is in a different package.

    Then you can call the methods like:
    Java Code:
    Tools.someToolsMethod();

  6. #6
    kizzie33 is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by demonick View Post
    Go to File -> Project properties
    that does exists, or at least not on the mac version

  7. #7
    kizzie33 is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Is this Tools class part of your project?

    If so then you should be able to just do an import in the class you want to use these methods, assuming Tools is in a different package.

    Then you can call the methods like:
    Java Code:
    Tools.someToolsMethod();
    ok so how do i import a different class :S ?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    What do you mean by import?
    "import" is a word with meaning in Java.

    Is the class already in your project?

    What IDE are you using?

  9. #9
    kizzie33 is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    What do you mean by import?
    "import" is a word with meaning in Java.

    Is the class already in your project?

    What IDE are you using?
    Right i said important because tolls said it "If so then you should be able to just do an import in the class"

    Im using eclipse and the class is in the same project as the other class.

    All i want is that when im compile my project the methods in a file are put into every class

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Right, so import the Tools class into the class(es) that need it.
    At the top of the class (after any package declaration):
    Java Code:
    import <whatever package it's in>.Tools;
    If they are in the same package, however, you don't even need to do an import.

    And then you can use the methods as I showed above.

Similar Threads

  1. Methods in Object class
    By Navatha in forum New To Java
    Replies: 1
    Last Post: 09-28-2010, 11:17 PM
  2. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  3. Concept about methods of Class
    By s021126 in forum New To Java
    Replies: 9
    Last Post: 08-25-2009, 04:23 AM
  4. Replies: 8
    Last Post: 03-03-2009, 08:28 AM
  5. Declare methods in a class
    By Adiel224 in forum New To Java
    Replies: 5
    Last Post: 09-19-2008, 10:38 AM

Posting Permissions

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