Re: constants in java
vipoperat@vip.hr wrote:
How to atributes define like constants in some class
be visible in other class?
i wrote..
public class Board
{
public static final int MAX_STU = 15;
public static final int MAX_RED = 10;
..
..
..
when I try to use MAX_STU and MAX_RED in other class (same package)
it didn't recognize this constants. Please help. Thanks
First, always provide code samples. "Try to use" is so vague - was there a
syntax error? Did the compiler complain or did you get a run-time error?
Please don't answer these questions here, this time - I know perfectly well
what the answers are. I just wanted to point out that in general you need to
provide much better detail in your questions.
(same package)
Let's call that package 'foo', shall we?
Using my powers of psychic SSCCE generation, I've come up with your client
code looking something like this:
<notSSCCE>
package foo;
public class BoardTester
{
public static void main( String [] args )
{
System.out.print( "Board MAX_STU = " );
System.out.println( MAX_STU );
}
}
Compiler error:
cannot find symbol
symbol : variable MAX_STU
</notSSCCE>
Two solutions:
Use 'import static' before the 'class' declaration for 'BoardTester' (or
whatever your class is):
import static Board.MAX_STU;
or the more usual, easier-to-understand 'Class.member' notation when you use
these members:
System.out.println( Board.MAX_STU );
--
Lew