I am using a free pdf book (Think Java - How to Think Like a Computer Scientist) and a Dummies book to try to teach myself Java. Eclipse mars is my IDE, using Java 8. I have encountered a problem with one of the examples in the Think Java book. I have spent days debugging and modifying the code, reading books, and Googling articles in an attempt to find a solution. Part of the code is provided, and the rest has to be figured out. The program takes two points defined by x,y coordinates and calculates the distance between the points using a custom method. Four variable values (x1, x2, y1, and y2) are passed to the method. The method performs the calculation. The method is supposed to return the value of the distance. Returning the resulting value is where I am hitting a dead end. The compiler error indicates that the result cannot be resolved to a variable. Included below is the last working code which prints various values from within the method. I have been unable to pass return the distance from the method to be printed from main.
Here is the error code:
Description Resource Path Location Type
result cannot be resolved to a variable DistanceBetweenTwoPoints.java /06-02-Example/src line 30 Java Problem
Here is the output from the method when the println statement in main is commented out:
dx is 3.0
dy is 4.0
dsquared is 25.0
distance is 5.0
How can I successfully return the value from the method?
Any helpful assistance will be greatly appreciated.
Will Klem wrote:Returning the resulting value is where I am hitting a dead end. The compiler error indicates that the result cannot be resolved to a variable.
You have the method "distance" which returns the result. So, think about it as your result being returned to line 28, from where you're calling it.
There are two options to solve that quickly:
1. on line 28 you do "double result = DistanceBetweenTwoPoints.distance(1.0, 2.0, 4.0, 6.0);" in this case "result" will hold the returned value
2. put line 28 inside line 30 print statement, it will return value and will print out without storing it.
I really appreciate having two options provided. This has taught me two more advanced concepts which may have taken me a long time to discover:
1) A method call can be assigned to a variable to obtain a return value.
2) A method call can be used as an argument s demonstrated with System.out.println().
I, too, have encountered errors in Think Java. Some I have figured out, and others I have just passed by.
Thanks to both of you for sharing your knowledge and experience with me.
And the third concept taught by CR latest post that "class name not needed to add prior the method" as it would work also only with "double result = distance(....);" OR "System.out.println(distance(....));" Your both methods are static, and you're using it in the same class, so, writing class name becomes redundant.
I think one other key thing you need to take from this is understanding scope. Your original code has a variable called result in the distance method, and you need to understand why this variable is not accessible inside the main method.
And what Mike just mentioned, in order to understand that, you can start by reading this Oracle tutorial (<- link to read).
 added quote
Oracle's tutorial wrote:Not all combinations of instance and class variables and methods are allowed:
Instance methods can access instance variables and instance methods directly.
Instance methods can access class variables and class methods directly.
Class methods can access class variables and class methods directly.
Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.