c++ - java style static constant initializationWhat's the C++ idiom equivalent to the Java static block?What are the differences between a pointer variable and a reference variable in C++?Are static class variables possible?Are there constants in JavaScript?The Definitive C++ Book Guide and ListDifference between static class and singleton pattern?Static readonly vs constEfficiency of Java “Double Brace Initialization”?Initialization of an ArrayList in one lineStatic constant string (class member)What is the “-->” operator in C++?
How do I minimise waste on a flight?
What's the difference between "ricochet" and "bounce"?
What is more safe for browsing the web: PC or smartphone?
Is there a reason why Turkey took the Balkan territories of the Ottoman Empire, instead of Greece or another of the Balkan states?
Why did Gendry call himself Gendry Rivers?
What's the 2-minute timer on mobile Deutsche Bahn tickets?
In a series of books, what happens after the coming of age?
Convert a huge txt-file into a dataset
What is the Ancient One's mistake?
How can I test a shell script in a "safe environment" to avoid harm to my computer?
Why doesn't increasing the temperature of something like wood or paper set them on fire?
Bash prompt takes only the first word of a hostname before the dot
What chord could the notes 'F A♭ E♭' form?
If quadruped mammals evolve to become bipedal will their breast or nipple change position?
Do the Zhentarim fire members for killing fellow members?
Which "exotic salt" can lower water's freezing point by 70 °C?
Picking a theme as a discovery writer
How can I finally understand the confusing modal verb "мочь"?
What calendar would the Saturn nation use?
My parents are Afghan
Crime rates in a post-scarcity economy
Why always 4...dxc6 and not 4...bxc6 in the Ruy Lopez Exchange?
My large rocket is still flipping over
All of my Firefox add-ons have been disabled suddenly, how can I re-enable them?
c++ - java style static constant initialization
What's the C++ idiom equivalent to the Java static block?What are the differences between a pointer variable and a reference variable in C++?Are static class variables possible?Are there constants in JavaScript?The Definitive C++ Book Guide and ListDifference between static class and singleton pattern?Static readonly vs constEfficiency of Java “Double Brace Initialization”?Initialization of an ArrayList in one lineStatic constant string (class member)What is the “-->” operator in C++?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am a newbie in c++. I am trying to create a static constant container in c++. In java we typically do that by static constant initialization. For e.g.
class ConstantDefinition
public static const List<String> stringList = new ArrayList<String>();
static
stringList.add("foo");
stringList.add("boo");
...blah
The way java works, I don't need to call a specific method to get the initialization done. Static block gets initialized once the class is loaded into JVM. But in c++ we don't have the same class loading mechanism as java.
And what I want is to have a single copy of non modifiable container that I can use without creating the class objects every time. One way I understand is that I create a class(similar to my java example above) and define a const static container. But I am finding it difficult to write that kind of code in C++ because I can't do the initialization without calling a method. So what's the best way to achieve this? The second approach could be that I define a header file and initialize global variables within namespaces. If I take this approach then would it create different global variable each time when I include that header file or the same one will be used?
Thanks,
RG
c++ static initialization constants
add a comment |
I am a newbie in c++. I am trying to create a static constant container in c++. In java we typically do that by static constant initialization. For e.g.
class ConstantDefinition
public static const List<String> stringList = new ArrayList<String>();
static
stringList.add("foo");
stringList.add("boo");
...blah
The way java works, I don't need to call a specific method to get the initialization done. Static block gets initialized once the class is loaded into JVM. But in c++ we don't have the same class loading mechanism as java.
And what I want is to have a single copy of non modifiable container that I can use without creating the class objects every time. One way I understand is that I create a class(similar to my java example above) and define a const static container. But I am finding it difficult to write that kind of code in C++ because I can't do the initialization without calling a method. So what's the best way to achieve this? The second approach could be that I define a header file and initialize global variables within namespaces. If I take this approach then would it create different global variable each time when I include that header file or the same one will be used?
Thanks,
RG
c++ static initialization constants
The part you don't need isnew
.
– Cody Gray♦
May 23 '14 at 13:34
1
Cody, what I have written is a java code, we do need new in java.
– user3669040
May 23 '14 at 13:40
Related: [What's the C++ idiom equivalent to the Java static block? ](stackoverflow.com/q/19227664/514235)
– iammilind
Mar 23 at 6:15
add a comment |
I am a newbie in c++. I am trying to create a static constant container in c++. In java we typically do that by static constant initialization. For e.g.
class ConstantDefinition
public static const List<String> stringList = new ArrayList<String>();
static
stringList.add("foo");
stringList.add("boo");
...blah
The way java works, I don't need to call a specific method to get the initialization done. Static block gets initialized once the class is loaded into JVM. But in c++ we don't have the same class loading mechanism as java.
And what I want is to have a single copy of non modifiable container that I can use without creating the class objects every time. One way I understand is that I create a class(similar to my java example above) and define a const static container. But I am finding it difficult to write that kind of code in C++ because I can't do the initialization without calling a method. So what's the best way to achieve this? The second approach could be that I define a header file and initialize global variables within namespaces. If I take this approach then would it create different global variable each time when I include that header file or the same one will be used?
Thanks,
RG
c++ static initialization constants
I am a newbie in c++. I am trying to create a static constant container in c++. In java we typically do that by static constant initialization. For e.g.
class ConstantDefinition
public static const List<String> stringList = new ArrayList<String>();
static
stringList.add("foo");
stringList.add("boo");
...blah
The way java works, I don't need to call a specific method to get the initialization done. Static block gets initialized once the class is loaded into JVM. But in c++ we don't have the same class loading mechanism as java.
And what I want is to have a single copy of non modifiable container that I can use without creating the class objects every time. One way I understand is that I create a class(similar to my java example above) and define a const static container. But I am finding it difficult to write that kind of code in C++ because I can't do the initialization without calling a method. So what's the best way to achieve this? The second approach could be that I define a header file and initialize global variables within namespaces. If I take this approach then would it create different global variable each time when I include that header file or the same one will be used?
Thanks,
RG
c++ static initialization constants
c++ static initialization constants
asked May 23 '14 at 13:33
user3669040user3669040
263
263
The part you don't need isnew
.
– Cody Gray♦
May 23 '14 at 13:34
1
Cody, what I have written is a java code, we do need new in java.
– user3669040
May 23 '14 at 13:40
Related: [What's the C++ idiom equivalent to the Java static block? ](stackoverflow.com/q/19227664/514235)
– iammilind
Mar 23 at 6:15
add a comment |
The part you don't need isnew
.
– Cody Gray♦
May 23 '14 at 13:34
1
Cody, what I have written is a java code, we do need new in java.
– user3669040
May 23 '14 at 13:40
Related: [What's the C++ idiom equivalent to the Java static block? ](stackoverflow.com/q/19227664/514235)
– iammilind
Mar 23 at 6:15
The part you don't need is
new
.– Cody Gray♦
May 23 '14 at 13:34
The part you don't need is
new
.– Cody Gray♦
May 23 '14 at 13:34
1
1
Cody, what I have written is a java code, we do need new in java.
– user3669040
May 23 '14 at 13:40
Cody, what I have written is a java code, we do need new in java.
– user3669040
May 23 '14 at 13:40
Related: [What's the C++ idiom equivalent to the Java static block? ](stackoverflow.com/q/19227664/514235)
– iammilind
Mar 23 at 6:15
Related: [What's the C++ idiom equivalent to the Java static block? ](stackoverflow.com/q/19227664/514235)
– iammilind
Mar 23 at 6:15
add a comment |
3 Answers
3
active
oldest
votes
There are several solutions, depending on how complicated the
actual initialization is, and whether you can count on C++11 or
not. In all cases, the solution depends on the fact that
constructors are called on static variables when the code is
loaded for execution.
In the simplest case, you just define the variable with an
initialzier, e.g.:
In the class:
class ConstantDefinition
static std::vector<std::string> const stringList;
// ...
;
(This will be the same for all of the solutions.)
And in a source file:
std::vector<std::string> const ConstantDefinition::stringList
"foo",
"boo",
// ...
This only works in C++11, however. With earlier versions of
C++, you'll need to define the variable:
std::string const stringListInit[] =
"foo",
"boo",
// ...
;
std::vector<std::string> const ConstantDefinition::stringList(
begin( stringListInit ), end( stringListInit ) );
You'll also need the functions begin
and end
:
template <typename T, size_t N>
T* begin( T (&array)[N} ) return array;
template <typename T, size_t N>
T* end( T (&array)[N] ) return array + N;
If you don't have C++11 (where they are in the standard
library), you'll want them anyway.
Don't forget that in either of the initializer lists, you can
use any arbitrary expression for the initialization, including
function calls.
Finally, if your initialization is too complex for this, you can
always encapsulate it in a function:
namespace
std::vector<std::string> stringListInit()
std::vector<std::string> results;
results.push_back( "foo" );
// ...
return results;
std::vector<std::string> const ConstantDefinition::stringList( stringListInit() );
Don't worry too much about creating an array which will be
copied and then destroyed; C++ allows something calls NRVO,
which means that the compiler can actually "merge" the local
variable in stringListInit
andConstantDefinition::stringList
, so there will only be onestd::vector<std::string>
actually constructed.
add a comment |
If you put this code in any compilation unit (probably a source file)
namespace
static struct Initialiser
Initialiser()
// ToDo - initialisation code here
TheInitialiser;
Then the code block will be ran once the library / executable is loaded (and before the main
function, if any, is called). This idiom is quite common.
The outer namespace
(called an anonymous namespace) block prevents this code being emitted into the linker and other compilation units.
If I define a vector inside the struct Initializer and initialize it inside the constructor, it appears to work. However I can't make the vector constant. So how to get around that restriction? In this case do I need to define this anonymous namespace in the same file that intends to use this struct or can it be defined in a separate source file? If I define it in a separate source file then should I do #include <source.cpp> ( Not sure but adding .cpp does not look okay to me in #include). Also, just for my understanding purposes, I think we can create more than one struct object, right?
– user3669040
May 23 '14 at 14:38
add a comment |
In ConstantDefinition.hpp :
#include <string>
#include <vector>
struct ConstantDefinition
static const std::vector<std::string> string_array;
;
In ConstantDefinition.cpp :
const std::vector<std::string> ConstantDefinition::string_array =
"foo",
"boo"
;
Note : C++11 or higher is required.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23830679%2fc-java-style-static-constant-initialization%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are several solutions, depending on how complicated the
actual initialization is, and whether you can count on C++11 or
not. In all cases, the solution depends on the fact that
constructors are called on static variables when the code is
loaded for execution.
In the simplest case, you just define the variable with an
initialzier, e.g.:
In the class:
class ConstantDefinition
static std::vector<std::string> const stringList;
// ...
;
(This will be the same for all of the solutions.)
And in a source file:
std::vector<std::string> const ConstantDefinition::stringList
"foo",
"boo",
// ...
This only works in C++11, however. With earlier versions of
C++, you'll need to define the variable:
std::string const stringListInit[] =
"foo",
"boo",
// ...
;
std::vector<std::string> const ConstantDefinition::stringList(
begin( stringListInit ), end( stringListInit ) );
You'll also need the functions begin
and end
:
template <typename T, size_t N>
T* begin( T (&array)[N} ) return array;
template <typename T, size_t N>
T* end( T (&array)[N] ) return array + N;
If you don't have C++11 (where they are in the standard
library), you'll want them anyway.
Don't forget that in either of the initializer lists, you can
use any arbitrary expression for the initialization, including
function calls.
Finally, if your initialization is too complex for this, you can
always encapsulate it in a function:
namespace
std::vector<std::string> stringListInit()
std::vector<std::string> results;
results.push_back( "foo" );
// ...
return results;
std::vector<std::string> const ConstantDefinition::stringList( stringListInit() );
Don't worry too much about creating an array which will be
copied and then destroyed; C++ allows something calls NRVO,
which means that the compiler can actually "merge" the local
variable in stringListInit
andConstantDefinition::stringList
, so there will only be onestd::vector<std::string>
actually constructed.
add a comment |
There are several solutions, depending on how complicated the
actual initialization is, and whether you can count on C++11 or
not. In all cases, the solution depends on the fact that
constructors are called on static variables when the code is
loaded for execution.
In the simplest case, you just define the variable with an
initialzier, e.g.:
In the class:
class ConstantDefinition
static std::vector<std::string> const stringList;
// ...
;
(This will be the same for all of the solutions.)
And in a source file:
std::vector<std::string> const ConstantDefinition::stringList
"foo",
"boo",
// ...
This only works in C++11, however. With earlier versions of
C++, you'll need to define the variable:
std::string const stringListInit[] =
"foo",
"boo",
// ...
;
std::vector<std::string> const ConstantDefinition::stringList(
begin( stringListInit ), end( stringListInit ) );
You'll also need the functions begin
and end
:
template <typename T, size_t N>
T* begin( T (&array)[N} ) return array;
template <typename T, size_t N>
T* end( T (&array)[N] ) return array + N;
If you don't have C++11 (where they are in the standard
library), you'll want them anyway.
Don't forget that in either of the initializer lists, you can
use any arbitrary expression for the initialization, including
function calls.
Finally, if your initialization is too complex for this, you can
always encapsulate it in a function:
namespace
std::vector<std::string> stringListInit()
std::vector<std::string> results;
results.push_back( "foo" );
// ...
return results;
std::vector<std::string> const ConstantDefinition::stringList( stringListInit() );
Don't worry too much about creating an array which will be
copied and then destroyed; C++ allows something calls NRVO,
which means that the compiler can actually "merge" the local
variable in stringListInit
andConstantDefinition::stringList
, so there will only be onestd::vector<std::string>
actually constructed.
add a comment |
There are several solutions, depending on how complicated the
actual initialization is, and whether you can count on C++11 or
not. In all cases, the solution depends on the fact that
constructors are called on static variables when the code is
loaded for execution.
In the simplest case, you just define the variable with an
initialzier, e.g.:
In the class:
class ConstantDefinition
static std::vector<std::string> const stringList;
// ...
;
(This will be the same for all of the solutions.)
And in a source file:
std::vector<std::string> const ConstantDefinition::stringList
"foo",
"boo",
// ...
This only works in C++11, however. With earlier versions of
C++, you'll need to define the variable:
std::string const stringListInit[] =
"foo",
"boo",
// ...
;
std::vector<std::string> const ConstantDefinition::stringList(
begin( stringListInit ), end( stringListInit ) );
You'll also need the functions begin
and end
:
template <typename T, size_t N>
T* begin( T (&array)[N} ) return array;
template <typename T, size_t N>
T* end( T (&array)[N] ) return array + N;
If you don't have C++11 (where they are in the standard
library), you'll want them anyway.
Don't forget that in either of the initializer lists, you can
use any arbitrary expression for the initialization, including
function calls.
Finally, if your initialization is too complex for this, you can
always encapsulate it in a function:
namespace
std::vector<std::string> stringListInit()
std::vector<std::string> results;
results.push_back( "foo" );
// ...
return results;
std::vector<std::string> const ConstantDefinition::stringList( stringListInit() );
Don't worry too much about creating an array which will be
copied and then destroyed; C++ allows something calls NRVO,
which means that the compiler can actually "merge" the local
variable in stringListInit
andConstantDefinition::stringList
, so there will only be onestd::vector<std::string>
actually constructed.
There are several solutions, depending on how complicated the
actual initialization is, and whether you can count on C++11 or
not. In all cases, the solution depends on the fact that
constructors are called on static variables when the code is
loaded for execution.
In the simplest case, you just define the variable with an
initialzier, e.g.:
In the class:
class ConstantDefinition
static std::vector<std::string> const stringList;
// ...
;
(This will be the same for all of the solutions.)
And in a source file:
std::vector<std::string> const ConstantDefinition::stringList
"foo",
"boo",
// ...
This only works in C++11, however. With earlier versions of
C++, you'll need to define the variable:
std::string const stringListInit[] =
"foo",
"boo",
// ...
;
std::vector<std::string> const ConstantDefinition::stringList(
begin( stringListInit ), end( stringListInit ) );
You'll also need the functions begin
and end
:
template <typename T, size_t N>
T* begin( T (&array)[N} ) return array;
template <typename T, size_t N>
T* end( T (&array)[N] ) return array + N;
If you don't have C++11 (where they are in the standard
library), you'll want them anyway.
Don't forget that in either of the initializer lists, you can
use any arbitrary expression for the initialization, including
function calls.
Finally, if your initialization is too complex for this, you can
always encapsulate it in a function:
namespace
std::vector<std::string> stringListInit()
std::vector<std::string> results;
results.push_back( "foo" );
// ...
return results;
std::vector<std::string> const ConstantDefinition::stringList( stringListInit() );
Don't worry too much about creating an array which will be
copied and then destroyed; C++ allows something calls NRVO,
which means that the compiler can actually "merge" the local
variable in stringListInit
andConstantDefinition::stringList
, so there will only be onestd::vector<std::string>
actually constructed.
answered May 23 '14 at 16:48
James KanzeJames Kanze
131k9139282
131k9139282
add a comment |
add a comment |
If you put this code in any compilation unit (probably a source file)
namespace
static struct Initialiser
Initialiser()
// ToDo - initialisation code here
TheInitialiser;
Then the code block will be ran once the library / executable is loaded (and before the main
function, if any, is called). This idiom is quite common.
The outer namespace
(called an anonymous namespace) block prevents this code being emitted into the linker and other compilation units.
If I define a vector inside the struct Initializer and initialize it inside the constructor, it appears to work. However I can't make the vector constant. So how to get around that restriction? In this case do I need to define this anonymous namespace in the same file that intends to use this struct or can it be defined in a separate source file? If I define it in a separate source file then should I do #include <source.cpp> ( Not sure but adding .cpp does not look okay to me in #include). Also, just for my understanding purposes, I think we can create more than one struct object, right?
– user3669040
May 23 '14 at 14:38
add a comment |
If you put this code in any compilation unit (probably a source file)
namespace
static struct Initialiser
Initialiser()
// ToDo - initialisation code here
TheInitialiser;
Then the code block will be ran once the library / executable is loaded (and before the main
function, if any, is called). This idiom is quite common.
The outer namespace
(called an anonymous namespace) block prevents this code being emitted into the linker and other compilation units.
If I define a vector inside the struct Initializer and initialize it inside the constructor, it appears to work. However I can't make the vector constant. So how to get around that restriction? In this case do I need to define this anonymous namespace in the same file that intends to use this struct or can it be defined in a separate source file? If I define it in a separate source file then should I do #include <source.cpp> ( Not sure but adding .cpp does not look okay to me in #include). Also, just for my understanding purposes, I think we can create more than one struct object, right?
– user3669040
May 23 '14 at 14:38
add a comment |
If you put this code in any compilation unit (probably a source file)
namespace
static struct Initialiser
Initialiser()
// ToDo - initialisation code here
TheInitialiser;
Then the code block will be ran once the library / executable is loaded (and before the main
function, if any, is called). This idiom is quite common.
The outer namespace
(called an anonymous namespace) block prevents this code being emitted into the linker and other compilation units.
If you put this code in any compilation unit (probably a source file)
namespace
static struct Initialiser
Initialiser()
// ToDo - initialisation code here
TheInitialiser;
Then the code block will be ran once the library / executable is loaded (and before the main
function, if any, is called). This idiom is quite common.
The outer namespace
(called an anonymous namespace) block prevents this code being emitted into the linker and other compilation units.
edited May 23 '14 at 13:47
answered May 23 '14 at 13:41
BathshebaBathsheba
184k27261387
184k27261387
If I define a vector inside the struct Initializer and initialize it inside the constructor, it appears to work. However I can't make the vector constant. So how to get around that restriction? In this case do I need to define this anonymous namespace in the same file that intends to use this struct or can it be defined in a separate source file? If I define it in a separate source file then should I do #include <source.cpp> ( Not sure but adding .cpp does not look okay to me in #include). Also, just for my understanding purposes, I think we can create more than one struct object, right?
– user3669040
May 23 '14 at 14:38
add a comment |
If I define a vector inside the struct Initializer and initialize it inside the constructor, it appears to work. However I can't make the vector constant. So how to get around that restriction? In this case do I need to define this anonymous namespace in the same file that intends to use this struct or can it be defined in a separate source file? If I define it in a separate source file then should I do #include <source.cpp> ( Not sure but adding .cpp does not look okay to me in #include). Also, just for my understanding purposes, I think we can create more than one struct object, right?
– user3669040
May 23 '14 at 14:38
If I define a vector inside the struct Initializer and initialize it inside the constructor, it appears to work. However I can't make the vector constant. So how to get around that restriction? In this case do I need to define this anonymous namespace in the same file that intends to use this struct or can it be defined in a separate source file? If I define it in a separate source file then should I do #include <source.cpp> ( Not sure but adding .cpp does not look okay to me in #include). Also, just for my understanding purposes, I think we can create more than one struct object, right?
– user3669040
May 23 '14 at 14:38
If I define a vector inside the struct Initializer and initialize it inside the constructor, it appears to work. However I can't make the vector constant. So how to get around that restriction? In this case do I need to define this anonymous namespace in the same file that intends to use this struct or can it be defined in a separate source file? If I define it in a separate source file then should I do #include <source.cpp> ( Not sure but adding .cpp does not look okay to me in #include). Also, just for my understanding purposes, I think we can create more than one struct object, right?
– user3669040
May 23 '14 at 14:38
add a comment |
In ConstantDefinition.hpp :
#include <string>
#include <vector>
struct ConstantDefinition
static const std::vector<std::string> string_array;
;
In ConstantDefinition.cpp :
const std::vector<std::string> ConstantDefinition::string_array =
"foo",
"boo"
;
Note : C++11 or higher is required.
add a comment |
In ConstantDefinition.hpp :
#include <string>
#include <vector>
struct ConstantDefinition
static const std::vector<std::string> string_array;
;
In ConstantDefinition.cpp :
const std::vector<std::string> ConstantDefinition::string_array =
"foo",
"boo"
;
Note : C++11 or higher is required.
add a comment |
In ConstantDefinition.hpp :
#include <string>
#include <vector>
struct ConstantDefinition
static const std::vector<std::string> string_array;
;
In ConstantDefinition.cpp :
const std::vector<std::string> ConstantDefinition::string_array =
"foo",
"boo"
;
Note : C++11 or higher is required.
In ConstantDefinition.hpp :
#include <string>
#include <vector>
struct ConstantDefinition
static const std::vector<std::string> string_array;
;
In ConstantDefinition.cpp :
const std::vector<std::string> ConstantDefinition::string_array =
"foo",
"boo"
;
Note : C++11 or higher is required.
answered May 23 '14 at 13:43
ChnossosChnossos
5,95411931
5,95411931
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23830679%2fc-java-style-static-constant-initialization%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The part you don't need is
new
.– Cody Gray♦
May 23 '14 at 13:34
1
Cody, what I have written is a java code, we do need new in java.
– user3669040
May 23 '14 at 13:40
Related: [What's the C++ idiom equivalent to the Java static block? ](stackoverflow.com/q/19227664/514235)
– iammilind
Mar 23 at 6:15