No.
public class UnitTestTwo { public static long factorial( int N ) { long fct = 1; for ( int j=1; j<=N; j++ ) fct *= j; return fct; } public static long factorialAlt( int N ) { long fct = 1; while ( N>0 ) { fct *= N; N-- ; } return fct; } public static void main (String[] args ) { int N = 10; System.out.println( "factorial of " + N ); System.out.println( "scnd method : " + factorialAlt( N )); System.out.println( "first method : " + factorial( N ) ); } }
C:\Source> java UnitTestTwo factorial of 10 scnd method : 3628800 first method : 3628800 C:\Source>
The new program (above) includes two static methods.
Of course, this is fine.
You can have as many static methods as needed.
The new static method also uses N
for the name of its formal parameter.
This is not a problem.
The scope of that N
is just that method's body.
The new N
does not interfere with the others.
Note in particular that the new method factorialAlt()
changes the value in its N
as it executes.
This is fine.
N
in main()
is a totally different
variable and keeps the value that was put in it.
Examine the following. Does this code compile? Pay particular attention to N
.
public class UnitTestBad { int N = 10; public static long factorial() { long fct = 1; for ( int j=1; j<=N; j++ ) fct *= j; return fct; } public static void main (String[] args ) { System.out.println( "factorial of " + N + ": " + factorial( N ) ); } }