Create a class in java and declare a String field like the following in that class,
public class MyConstants {
public static final String Url = "http://192.1.1.1/Service1.asmx";
}
then you can access it globally from any class like the following
String url = MyConstants.Url;
As Url
is a static final field of MyConstants class, so you can access it just with the name of the class(without creating the object of the class with new
Operator). i.e MyConstants
in this case.
For Learning more about static
and to see how this thing works, Please refer to this link
final
means that the value cannot be changed after initialization, that's what makes it a constant. static
means that instead of having space allocated for the field in each object, only one instance is created for the class.
So, static final
means only one instance of the variable no matter how many objects are created and the value of that variable can never change.