View Full Version : new to java
mesman00
February 25th, 2004, 03:16
hi. i'm new to java, and was wondering if there was a define statement like in c.
for example, in c i can define something as follows:
#define SIZE 40
Now everytime the compiler sees SIZE i will substitute the integer 40. Is there someway to do this in java? thanks.
Teamz
February 25th, 2004, 03:44
the word 'final' is reserved for constants
mesman00
February 25th, 2004, 03:47
the word 'final' is reserved for constants
an example of how to use it?
domi_fan
February 25th, 2004, 05:58
public static final int SIZE = 40;
unfortunately, this will then only be available as SIZE within the class it is declared. If you want to use it in other classes, you'll need to qualify it with the class name that it was declared in
for example assume this:
public class Foo
{
public static final int SIZE = 40;
public void doSomething()
{
//Here we can refer to the constant as just 'SIZE'
System.out.println( "The value of size is: " + SIZE );
}
}
but when we are in another class it works like this:
public class Bar
{
public void doSomethingElse()
{
//Here we must qualify the constant with the name of the class
// that it was declared in.
System.out.println( "The value of size is: " + Foo.SIZE );
}
}
Note that this is not a compiler thing. If I had it within a String literal, it wouldn't change to 40.
Hope this helps,
Domi_fan
new_profile
February 25th, 2004, 13:43
Hi,
Give a try to the new functionnality of Java 1.5 (still in beta though) : static import.
Thank you.
domi_fan
February 25th, 2004, 15:23
Hi,
Give a try to the new functionnality of Java 1.5 (still in beta though) : static import.
Thank you.
I'm not so sure if you're just starting out that you want to be using a beta JDK. It's hard enough flushing out bugs in your own software without having to worry about running into bugs in the platform.
Just a thought,
Domi_fan
mesman00
February 26th, 2004, 00:33
the word 'final' is reserved for constants
i see, i also realize that i am an idiot and didn't understand your post at first. thanks though for the example that reinforces that i am dumb.
Teamz
February 26th, 2004, 10:41
i see, i also realize that i am an idiot and didn't understand your post at first. thanks though for the example that reinforces that i am dumb.
bah my english is bad so I usually shrink my sentences heh
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.