static final variables like with Java

static final variables like with Java

Post by Michael J. Bergi » Fri, 20 Jun 2003 02:08:29



In the Java language I can use a static final variable as a typed constant:

public class Color {
    public static final Color RED = new Color("Red");
    public static final int  DEFAULT_R_VALUE = 150;

Quote:}

Then in another class I can do this

public class TextBox {
    public Text getText( ) {
        Text text = new Text("Hello World", Color.RED);
    }

Quote:}

How do I represent this same type of constant in Objective-C?  The
requirements I image for it are:

1.  The value should exist only once in memory, at least as it is it is with
Java, if the runtime creates multiple copies that it manages that's fine but
not an instance variable.
2.  It should not be changeable at runtime, like the final in Java.
3.  It should be statically typed, not a #define...

If this isn't possible what's the next best thing?

 
 
 

static final variables like with Java

Post by Michael As » Fri, 20 Jun 2003 02:20:36




Quote:> In the Java language I can use a static final variable as a typed constant:

> public class Color {
>     public static final Color RED = new Color("Red");
>     public static final int  DEFAULT_R_VALUE = 150;
> }

> Then in another class I can do this

> public class TextBox {
>     public Text getText( ) {
>         Text text = new Text("Hello World", Color.RED);
>     }
> }

> How do I represent this same type of constant in Objective-C?  The
> requirements I image for it are:

You have a couple of options. First, the static global! In your .h file,
put extern declarations like so:

extern const int foo;
extern NSString * const happyString;

And then in your .m file:

const int foo = 42;

The main disadvantage of this is that they aren't really part of your
class, they just happen to live in the same file. You access them like
any global variable, directly by name. So you need to name them
carefully to indicate their relationship to your class, and to prevent
name conflicts.

Another way to do it is to simply have a bunch of class methods that
return things. See Cocoa's NSColor for an example on how this works.
When I want a blue NSColor, there's no global constant, but I can just
say [NSColor blueColor] and I'm all set. The advantage is that the
naming is clearer, you don't have name conflicts, and it's much less
ugly. The disadvantage is that none of this stuff is optimized, so you
incur the oveheard of a message send for every call.

 
 
 

static final variables like with Java

Post by Michael J. Bergi » Fri, 20 Jun 2003 04:39:40






>> In the Java language I can use a static final variable as a typed constant:

>> public class Color {
>>     public static final Color RED = new Color("Red");
>>     public static final int  DEFAULT_R_VALUE = 150;
>> }

>> Then in another class I can do this

>> public class TextBox {
>>     public Text getText( ) {
>>         Text text = new Text("Hello World", Color.RED);
>>     }
>> }

>> How do I represent this same type of constant in Objective-C?  The
>> requirements I image for it are:

> You have a couple of options. First, the static global! In your .h file,
> put extern declarations like so:

> extern const int foo;
> extern NSString * const happyString;

> And then in your .m file:

> const int foo = 42;

> The main disadvantage of this is that they aren't really part of your
> class, they just happen to live in the same file. You access them like
> any global variable, directly by name. So you need to name them
> carefully to indicate their relationship to your class, and to prevent
> name conflicts.

> Another way to do it is to simply have a bunch of class methods that
> return things. See Cocoa's NSColor for an example on how this works.
> When I want a blue NSColor, there's no global constant, but I can just
> say [NSColor blueColor] and I'm all set. The advantage is that the
> naming is clearer, you don't have name conflicts, and it's much less
> ugly. The disadvantage is that none of this stuff is optimized, so you
> incur the oveheard of a message send for every call.

So how do you think this would best be implemented?  (The second tip of
yours not the global variable)

static const MAX_CURSORS = 10

- (int)maxCursors {
    return MAX_CURSORS;

Quote:}

OR

- (int)maxCursors {
    static const MAX_CURSORS = 10;      /* Is this valid? */
    return MAX_CURSORS;

- Show quoted text -

Quote:}

 
 
 

static final variables like with Java

Post by Marcel Weih » Fri, 20 Jun 2003 07:47:32


["static class variables"]

>You have a couple of options. First, the static global! In your .h file,
>put extern declarations like so:
>extern const int foo;
>extern NSString * const happyString;
>And then in your .m file:
>const int foo = 42;

>The main disadvantage of this is that they aren't really part of your
>class, they just happen to live in the same file. You access them like
>any global variable, directly by name. So you need to name them
>carefully to indicate their relationship to your class, and to prevent
>name conflicts.

Another disadvantage is that this isn't automatically available to
bridged languages without * symbol hacking techniques.

Quote:>Another way to do it is to simply have a bunch of class methods that
>return things. See Cocoa's NSColor for an example on how this works.
>When I want a blue NSColor, there's no global constant, but I can just
>say [NSColor blueColor] and I'm all set. The advantage is that the
>naming is clearer, you don't have name conflicts, and it's much less
>ugly. The disadvantage is that none of this stuff is optimized, so you
>incur the oveheard of a message send for every call.

Much preferred, unless you actually find (via profiling, right?) that
the message overhead is a problem...

Marcel

 
 
 

static final variables like with Java

Post by Marcel Weih » Fri, 20 Jun 2003 07:50:08



Quote:>So how do you think this would best be implemented?  (The second tip of
>yours not the global variable)
>static const MAX_CURSORS = 10
>- (int)maxCursors {
>    return MAX_CURSORS;
>}
>OR
>- (int)maxCursors {
>    static const MAX_CURSORS = 10;      /* Is this valid? */
>    return MAX_CURSORS;
>}

You'd probably want to use a class method, not an instance method.
Furthermore, 1 level of indirection should probably be enough,
right?  So:

+(int)maxCursors { return 10; }

This may look like you're embedding a magic numerical constant
into code, but it really already is well-encapsulated and has
the intention-revealing-naming goodnes...

Marcel

 
 
 

1. Static variables and ordinary variables

I would like to know the difference between static variables and
variables
without the keyword static in a multithreaded environment.
Let us assume

static int StaticVar;
int OrdVar;
main()
{
    /*    create 10 threads in main */
    .pthreadcreate(..., thread_routine, ....)
    .
    .
thread_routine()
{

what is the memory image for the threads. how differently do the thread
treat these two variables.
-Karthik

2. xsd:choice of Attributes instead of Elements??

3. static variables in DLLs and local variables in FORTRAN

4. Motorola Flip Phone and Low Battery

5. static variables vs global variables vs performance

6. Need to run keyboard (ADB) and monitor 50 feet away from Mac II

7. Static Variable using Another Static problem

8. Converting .bmp files to .pict

9. static variables declared inside of non-static member functions

10. static variables inside non-static member functions

11. static variables in static libraries

12. C++ equivalent for FINAL keyword in JAVA?

13. Implementing Equivalent Final Java class in c++