What's new

new to java

mesman00

What's that...?
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:
Code:
#define SIZE 40
Now everytime the compiler sees SIZE i will substitute the integer 40. Is there someway to do this in java? thanks.
 

domi_fan

New member
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:

Code:
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:

Code:
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
 

domi_fan

New member
new_profile said:
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
 
OP
mesman00

mesman00

What's that...?
Teamz said:
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

J'aime tes seins
mesman00 said:
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
 

Top