Results 1 to 14 of 14
  1. #1
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Question Calling a method? <solved>

    Hi, I wrote my own method in Java, and I have it in it's own class. I am trying to call it from another class in the same package, but it gives me the error 'symbol not found'. What did I do wrong?
    Last edited by ngc0202; 08-03-2010 at 01:42 PM.

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

    Default

    You've got a bug in your code, simple as that. And without being able to see your code and the actual error message, we've no way of knowing what it is.

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

    Default

    That compiler error means that whatever symbol it is talking about (which will be there with the error, and will be a variable or method or class name usually) is not something you have given it. This is often a typo, like not using correct capitalisation (Java is case sensitive).

  4. #4
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Post

    Look at the mehod and where it is called. you will find what's causing error. For example you might have done like this :
    Java Code:
    public void myNumber(){
    // code here
    }
    
    mynumber(); // you get error
    Or
    Java Code:
    public void myNumber(int i){
    // code here
    }
    
    myNumber(); // you get error
    if still error exist then post your code and error msg.

    -regards

  5. #5
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    There was no typo. The code for the method (in a different file) is:
    PHP Code:
    public String method(String string, int int) {
    and for the calling, it's:
    PHP Code:
    System.println(method("string", 42));
    and the error is:
    cannot find symbol
    symbol : method method(java.lang.String,int)
    If you need more, just ask, I don't know how much of the code you want. The method files is 150+ lines.
    Last edited by ngc0202; 08-03-2010 at 01:26 PM. Reason: Added error

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    The code should be:
    Java Code:
    public class A{
      public String method(String string, int anInt) {  
        return "Blah!";
    }
    
    public class B{
      public void something(){
        A aObject = new A();
        System.out.println(aObject.method("string", 42));  
      }
    }
    Disregarding that your example is far away from being compilable.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    The only part I don't have is the A aObject = new A();
    What's that for?

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Oh dear lord. Back to the basic tutorials for you.
    Trail: Learning the Java Language (The Java™ Tutorials)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    Ohh, what should the aObject be then? It uses the arguments in the method(), not an Object.

  10. #10
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    If the method you are calling is in another class, and is not static, you need to instantiate a new object and then use that to call the method.

    Java Code:
    MyClass myObject = new MyClass ();
    
    System.out.println (myObject.method ("string", 42));
    Alternatively, you can make the method "static", which means that you can call it without instantiating an instance of the class...

    Java Code:
    public static String method (String string, int int) {
        // code here
    }
    Java Code:
    System.out.println (MyObject.method ("string", 42);
    Hope this helps.
    Berkeleybross

  11. #11
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Wow reading a complete tutorial in three minutes, that must be some kind of record. ;)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  12. #12
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    @Berkeleybross Thanks, that helped, I didn't realise that I needed to have a caller object.
    @PhHein Thanks anyway, I never heard of that kind of object. btw I really like your picture.

    Also, follow up question, can I call a method from a project that doesn't have that class file in it? If so, how?
    Last edited by ngc0202; 08-03-2010 at 01:46 PM.

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

    Default

    You really need to go through the tutorials.

    You shouldn't be focussing on methods. You should be thinking in terms of classes and objects. The methods are simply the things you call on an object.

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

    Default

    Quote Originally Posted by ngc0202 View Post
    @Berkeleybross Thanks, that helped, I didn't realise that I needed to have a caller object.
    @PhHein Thanks anyway, I never heard of that kind of object. btw I really like your picture.
    "that kind of object"? PhHein is not talking about anything special, just basic Java programming, nothing more. If you don't understand this, then you really need to study the basics before trying to do anything else.

    Also, follow up question, can I call a method from a project that doesn't have that class file in it? If so, how?
    What's a "project"? If you call a method, you need to import the class if that's what you mean. Again, all of this is explained in introductory text books and tutorials.

Similar Threads

  1. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  2. Calling a method
    By mnki23 in forum New To Java
    Replies: 21
    Last Post: 11-06-2009, 09:10 PM
  3. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. method calling?
    By frejon26 in forum New To Java
    Replies: 4
    Last Post: 01-25-2008, 03:38 AM

Tags for this Thread

Posting Permissions

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