Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

constants in java

Thread view: 
vipoperat@vip.hr - 03 Jan 2008 14:51 GMT
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
Lew - 03 Jan 2008 15:18 GMT
> How to atributes define like constants in some class
> be visible in other class?
[quoted text clipped - 11 lines]
> 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 );

Signature

Lew

Wayne - 03 Jan 2008 20:29 GMT
>> How to atributes define like constants in some class
>> be visible in other class?
[quoted text clipped - 54 lines]
>
>  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
Mark Jeffcoat - 03 Jan 2008 19:22 GMT
> How to atributes define like constants in some class
> be visible in other class?
[quoted text clipped - 11 lines]
> 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.

Signature

Mark Jeffcoat
Austin, TX

Roedy Green - 04 Jan 2008 03:07 GMT
>public class Board
>{
[quoted text clipped - 6 lines]
>when I  try to use MAX_STU and MAX_RED in other class (same package)
>it didn't recognize this constants. Please help. Thanks

To use them, you must call them Board.MAX_STU or you must use import
static.  See http://mindprod.com/jgloss/constant.html#IMPORT

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

none - 25 Jan 2008 18:10 GMT
Write it this way: Board.MAX_STU

> How to atributes define like constants in some class
> be visible in other class?
[quoted text clipped - 11 lines]
> when I  try to use MAX_STU and MAX_RED in other class (same package)
> it didn't recognize this constants. Please help. Thanks


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.