Results 1 to 8 of 8
  1. #1
    sp725 is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default cannot find symbol error, defined inside private method

    hello, i am new to java and am looking for help with this problem. thank you very much to anyone who wouldn't mind helping me out.
    i need to use variables (r,x1,x2,etc.) inside the main method of my program that were previously defined inside a private method.
    is this possible, and if so how would i do this?

    example:

    private static double dist(double x1, double y1, double x2, double y2){

    double r;
    r = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    return r;
    }
    public static void main(String() args){
    forceX[body]= F*((x1-x2)/r);
    forceY[body]= F*((y1-y2)/r);

    }

    if it isn't possible, are there any changes i can make so that i'll be able to use these values later on?
    Last edited by sp725; 10-25-2011 at 03:17 AM.

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: cannot find symbol error, defined inside private method

    You can only call private methods from within their class. So as long as you're calling it from the same class, then yes you can.

    For the "cannot find symbol" error, please post the exact error message with the code that's causing it.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: cannot find symbol error, defined inside private method

    Quote Originally Posted by Solarsonic View Post
    You can only call private methods from within their class.
    That is not 100% correct. The following code will not compile.
    Java Code:
    public class Test {
        public static void main(String[] args) {
            XYZ.dostuff();
        }
    }
    
    class XYZ {
        public static void dostuff() {
            showMe();
        }
        
        private void showMe() {
            System.out.println("Hello World");
        }
    }

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: cannot find symbol error, defined inside private method

    Quote Originally Posted by Junky View Post
    That is not 100% correct. The following code will not compile.
    Java Code:
    public class Test {
        public static void main(String[] args) {
            XYZ.dostuff();
        }
    }
    
    class XYZ {
        public static void dostuff() {
            showMe();
        }
        
        private void showMe() {
            System.out.println("Hello World");
        }
    }
    Well of course, I don't see how that disproves my statement? The only place a private method can be called is within the class... the code won't compile because it's being called outside of its class.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: cannot find symbol error, defined inside private method

    No its not. The code is calling showMe from within the XYZ class.

  6. #6
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: cannot find symbol error, defined inside private method

    Quote Originally Posted by Junky View Post
    No its not. The code is calling showMe from within the XYZ class.
    Well that's because you're making a static reference to a nonstatic method... I never said that it can always be called there, just that it's the only place... but I definitely see your point.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: cannot find symbol error, defined inside private method

    But according to your comment I can do that because it is within the class. But we know that you cannot do that which brings us back to my point that your comment was not 100% correct.

  8. #8
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: cannot find symbol error, defined inside private method

    Quote Originally Posted by Junky View Post
    But according to your comment I can do that because it is within the class. But we know that you cannot do that which brings us back to my point that your comment was not 100% correct.
    My statement was:
    Java Code:
    You can only call private methods from within their class.
    not
    Java Code:
    You can always call private methods from within their class.
    You're absolutely correct just a little misreading :P It's late so I forgive you.
    Last edited by Solarsonic; 10-25-2011 at 04:51 AM.

Similar Threads

  1. Replies: 2
    Last Post: 03-08-2011, 03:08 PM
  2. HELP! can't find symbol-method
    By Jack9333 in forum New To Java
    Replies: 2
    Last Post: 03-04-2011, 01:48 AM
  3. Replies: 10
    Last Post: 12-22-2010, 01:12 AM
  4. cannot find symbol - method iterator()
    By D.Calladine in forum New To Java
    Replies: 1
    Last Post: 11-30-2010, 12:13 PM
  5. Replies: 9
    Last Post: 10-18-2008, 08:26 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
  •