The moose likes Java in General and the fly likes private static final String and concatenation question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "private static final String and concatenation question" Watch "private static final String and concatenation question" New topic
Author

private static final String and concatenation question

Edward Winchester
Ranch Hand

Joined: Sep 16, 2009
Posts: 65
Hello,

If I have a private static final String in a class, and that String was assigned the result of string concatenation, is it defined whether the concatenation occurs at runtime, or compile time?

e.g.


I guess it doesn't really matter because the cost is a one-off initialisation cost, but it was something I was wondering about.

Ed
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16692
    
  19


It depends on whether the expression (concat) is a compile time constant or not.

In this case, it is, and hence, the concat occurs at compile time. In fact, since you are assigning it to a final variable at declare time, the static variable is a compile time constant variable. This means that it can be used in another expression, with other constants, and it will still be a compile time constant expression.

Henry




Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Edward Winchester
Ranch Hand

Joined: Sep 16, 2009
Posts: 65
Thank you Henry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private static final String and concatenation question
 
Similar Threads
Looking for explanation
Bind class to HttpSession?
Using constants in GUI is this overkill?
Which way to initialize array
append a file name with timestamp