constants in java

V

vipoperat

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
 
L

Lew

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 );
 
M

Mark Jeffcoat

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

When you're in another class in the same package, you'll need to
refer to your constants as Board.MAX_STU and Board.MAX_RED.
 
W

Wayne

Lew said:
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 );

Just wanted to add that such constants are resolved at compile
time, not run time. If you change these, you need
to recompile every class that uses those constants or they
won't see the change. This can be a "gotcha" if you're not
careful.

-Wayne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top