9 Replies - 12089 Views - Last Post: 26 October 2009 - 08:14 PM Rate Topic: -----

#1 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Java final static vs static final

Posted 26 October 2009 - 07:58 AM

The end result of these two lines is the same:

static final String MY_CONST = "Hello";
final static String YOUR_CONST = "World";


but my question is: In general is there a semantic difference between an object declared "final static" vs "static final"?
Is This A Good Question/Topic? 0
  • +

Replies To: Java final static vs static final

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10809
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Java final static vs static final

Posted 26 October 2009 - 08:08 AM

I don't believe so because you are assigning the same properties to the String. It is in much the same way as the order doesn't matter if you set a JFrame's title before size or size before default close operation. Hope this helps some! :)
Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Java final static vs static final

Posted 26 October 2009 - 08:09 AM

Seems like more of a style question. Typically, I try to use static final. I try to put final as the first possible modifier after the access modifier and static (if needed).

Ex: public static final int five = 5;

I don't think there's an actual difference. It's all just modifiers, which can come in any order...

This post has been edited by Locke: 26 October 2009 - 08:11 AM

Was This Post Helpful? 0
  • +
  • -

#4 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: Java final static vs static final

Posted 26 October 2009 - 08:14 AM

No, Java don't care which order you put the modifiers in. So "public static" and "static public" both work and means the same semantically.
Was This Post Helpful? 0
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Java final static vs static final

Posted 26 October 2009 - 09:34 AM

i also think that the semantics don't differ, they mean the same thing..
Was This Post Helpful? 0
  • +
  • -

#6 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Java final static vs static final

Posted 26 October 2009 - 09:50 AM

actually they differ but only in the case of public static [the access specifier should and is given preference in java] but in case of modifiers you can follow the order you want

This post has been edited by sakshamkum: 26 October 2009 - 09:51 AM

Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Java final static vs static final

Posted 26 October 2009 - 10:36 AM

I am glad that my thoughts seem to be the consensus.


I recently discovered that I did not really understand the final modifier as well as I thought and so I have been a little uncomfortable with it recently.

I have been seeing final variables used in code a good deal recently and to tell the truth I really don't understand the point of this:

void function(int value) {
final MutableClass m = new MutableClass(value);
...
} 


What is the point of making a local variable final? I guess it is just to keep yourself from assigning it a new object months later when you need to update that function. I *thought* that it must mean that the object was constant... but no, a mutable object does not magically become immutable when declared final... Anyway, it has given me pause.
Was This Post Helpful? 0
  • +
  • -

#8 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Java final static vs static final

Posted 26 October 2009 - 11:26 AM

If my understanding of declaring objects final is correct (which it may not be, I'm a little rusty on it), it is that the reference stays constant. The final object cannot be assigned to another object, changing the reference. AKA, it cannot be used to alias another object. The fields of that final object are still, however, editable.

I think that's correct, though.

This post has been edited by Locke: 26 October 2009 - 11:26 AM

Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Java final static vs static final

Posted 26 October 2009 - 11:53 AM

Locke has it. Declaring things final prevents the reference from changing. When I'm programming, I declare everything as final unless there is a reason for me to modify it. It was a bit of a pain at the start but now it helps keep me straight, similar to strict typing.

One time it really comes into its own though is multithreading. If your class is declared final and all its data members are final, any data structures you return are put through Collections.immutableList() (or similar) then your class is thread safe without synchronisation.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Java final static vs static final

Posted 26 October 2009 - 08:14 PM

View Postmostyfriedman, on 26 Oct, 2009 - 08:34 AM, said:

i also think that the semantics don't differ, they mean the same thing..

yha
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1