• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing Values Between Methods 3  RSS feed

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to Ernest I took on the idea of creating a class in which to return multiple values of different types.

One class i have reads the first line of the file and then tries to pass this into a class called FirstLine. This is shown below



As you can see, i want to pass the first line of the file into a class (called FirstLine) where i want to calculate the length of the line and then create an array. These are then to be returned to the TestMethod() method.

The code for the class in which i am manipulating the string is shown below



I want these to be returned into the TestMethod() above so i can print them (to check that it is working). However, when i compile the Computation class, it says that it does not recognise the variable 'ThisFirstLine'. Where am i going wrong here?

[ November 01, 2005: Message edited by: Sam Bluesman ]
[ November 01, 2005: Message edited by: Sam Bluesman ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ThisFirstLine is a local variable declared in the method OpenFile; you're trying to access this variable in TestMethod(), where it's not available. Local variable names exist only inside the method that creates them.

Note that no code anywhere calls OpenFile anyway; presumably TestMethod should call it. Why not have OpenFile return the instance of FirstLine? Then TestMethod says:

FirstLine ThisFirstLine = OpenFile("filename");

Now, with that out of the way, let me point out to you that the Java language uses a very strong naming convention, in which the capitalization of a name tells you what kind of name it is. This makes reading code much easier, especially for experienced programmers. Using it will make it easier for people to help you!

The most important things to know is that ClassNamesLookLikeThis, but variableAndMethodNamesLookLikeThis -- i.e., always capitalize class names, but never capitalize variable or method names. The only exception to this rule is that CONSTANTS_LOOK_LIKE_THIS -- i.e., if you declare a "static final" variable, you should name it using all capital letters, especially if it's a primitive (int, double, char, boolean, etc) or a String.

In the code I typed above, I was careful to use your names for things, to make it easier for you to understand -- but it looks very strange to me, as I'd always write

FirstLine thisFirstLine = openFile("filename");
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!