0

This is my Config class:

public class Config
{
  public static final String urlApi = "http://127.0.0.1/api/";
}

Whenever I want to change the location, I have to change the value of this variable:

public class Config
{
  public static final String urlApi = "http://192.168.50.101/api/";
}

In this case, the value is:

"http://192.168.50.101/api/"

But in debugging mode I saw that "urlApi" has the old value, old IP address. Its weird. How do I fix it ?

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
TariqN
  • 668
  • 2
  • 9
  • 27
  • The value will never change if you set it as final. – Rabbit Guy May 06 '16 at 18:51
  • So if i say: urlApi = "something new" and buid project, urlApi will never be "something new" ? – TariqN May 06 '16 at 18:53
  • 2
    Absolutely not. If a variable is declared as final it means that it's initialized only one time – Lorenzo Barbagli May 06 '16 at 18:54
  • if the line above is somewhere else in your code, you cannot change it's value after you initially set it. It is `final` – Rabbit Guy May 06 '16 at 18:54
  • If you write `urlApi = "something new"`, you will get a compiler error. That's what `final` means. – SLaks May 06 '16 at 18:55
  • 1
    Think of final as meaning it is a constant. Once you set it, it's done. You cannot change what that variable references. – Rabbit Guy May 06 '16 at 18:56
  • What part of the word final do you not understand? – Pomagranite May 06 '16 at 18:58
  • I'm a beginner, and first time work with final variable, i don't get it. I stop the program, change hard-coded string, and build again, the value is not changed. – TariqN May 06 '16 at 19:01
  • Final refers to runtime ?, we can't change that variable in runtime, that is my understanding. – TariqN May 06 '16 at 19:04
  • 1
    Compile time constants. They are fun: http://stackoverflow.com/questions/5173372/java-static-final-values-replaced-in-code-when-compiling - not making them final actually helps but there are other ways around. Like passing them through a method that does nothing to them before assigning them to their `final` destination. – zapl May 06 '16 at 19:05
  • Maybe you didn't save the ```.java``` file before rebuilding. – Jorn Vernee May 06 '16 at 19:06
  • 3
    Why did I receive so many minuses, I think some didn't understand my question. :) – TariqN May 06 '16 at 19:09
  • @TariqN, I guess you got the downvotes because your question is not clear. Apart from the last word in the questions title you do not mention that the problem occurs after _building_ your code with the new configuration. Most probably assume you want to change it at runtime (which obviously fails because of the `final`). – siegi Jun 19 '16 at 14:54

3 Answers3

2

I solved the problem. I go to "Clean project" and then again "Make Project", i think this is explanation.

Note: If a primitive type or a string is defined as a constant and the value is known at compile time, the compiler replaces the constant name everywhere in the code with its value. This is called a compile-time constant. If the value of the constant in the outside world changes (for example, if it is legislated that pi actually should be 3.975), you will need to recompile any classes that use this constant to get the current value.

TariqN
  • 668
  • 2
  • 9
  • 27
  • The assumption about the compile time constant in your answer is probably correct. A decent IDE should detect this situation though and trigger a rebuild of the affected classes. – siegi Jun 19 '16 at 14:56
1

Take a look: http://www.tutorialspoint.com/java/java_nonaccess_modifiers.htm

There are the different Access Modifiers

Plex
  • 75
  • 2
  • 10
0

You must change:

public class Config {
    public static final String urlApi = "http://192.168.50.101/api/";
}

to:

public class Config {
    public static String urlApi = "http://192.168.50.101/api/";
}

The final keyword means that the String is a constant - not a variable. This is, it can't be changed. Removing final will allow the String to be changed whenever you want as normal.

Bobby
  • 1,416
  • 1
  • 17
  • 30