954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem static final

Excuse me, is I am new to Java and want to do an attribute (final)and static.

The problem is that first I need to instantiate and then, using a method to give its final value.

I need something this:

public static final int MyConstant;

public void createConstant(int pValue){
        MyConstant  = pValue;
    }


appreciate your help

suta123
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

A "final" variable can only be defined once, and when it is static it needs to be defined at the first reference to the Class. Which means that you won't be able to use a method to set the value of a static final variable. You could use a static block (which gets executed at class load time), but that's not quite the same thing. So, if it is going to be set by a method the variable can be static but it cannot be final, or it can be final (if the method is called by the constructor of the class), but it cannot be static.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: