English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

1)
I have private methods of a class and I need to use variables created by these private methods in other private methods of the same class. Now, should I declare the variable outside of the method at the beginning of the class and be done? Or do I need a public, static in front of it?

2)
I keep getting an error with a stringtokenizer
Exception in thread "main" java.util.NoSuchElementException
and java.util.StringTokenizer.nextToken(Unknown Source)

my code at the line is as follows....
headerArray.add( Integer.parseInt( header.nextToken()) )

header is a stringtokenizer, headerArray is an arraylist of ints.
Only thing i can think of is its not finding the input file which it is in the correct directory

2007-09-27 04:26:36 · 2 answers · asked by jay d 1 in Computers & Internet Programming & Design

the error should read.
java.util.StringTokenizer.nextToken(Unknown Source)

2007-09-27 04:27:13 · update #1

2 answers

1) Variables declared inside a method are called "temporary variables" because they exist only while the method is called. A new copy of the variable is created every time the method is called.

Variables declared outside of any method in the class are instance variables, which are shared among all methods of the class. There's only one copy of the variable per instance of the class, and the variable exists for the entire duration of each instance's existence.

Variables declared 'static' are shared to an even higher degree, there is only one copy of the variable that is shared among all instances of the class.

If you have private methods that need access to variables that are currently temporary variables in the calling method (on the same class), you will want to do one of the following:
(a) declare the variables as instance variables.
(b) pass the variables in to the other private methods, as method parameters.

Whether you do (a) or (b) depends on whether the variables' values have meaning for the entire duration of the instance's existence (instance vairable), or instead if they have meaning only in the context of the method call (temporary variable).

You might also have to consider threading issues. What happens if two separate threads call the first private method? If you use temp variables, each thread will have its own independent copy of the variable with its own value. If you use an instance variable, the same single value will be written/read by both threads.

(2) You can't call nextToken() unless hasMoreTokens() returns true. You can skip the hasMoreTokens() check if you are sure that the String has more tokens to parse, but if you run off the end of hte input, it throws an exception.

2007-09-27 05:51:09 · answer #1 · answered by McFate 7 · 0 0

1) You should always declare variables used as an internal state as private... and if needed, provide accessor methods. The compiler will throw an error if you need to declare them static

2) Are you checking with hasMoreElements() correctly? Surely you'd get a FileNotFoundException if it didn't load the file.

2007-09-27 11:54:07 · answer #2 · answered by kirun 6 · 0 0

fedest.com, questions and answers