Public Static Variable Value

Public static variable value

You can't use expressions when declaring class properties. I.e. you can't call something() here, you can only use static values. You'll have to set those values differently in code at some point.

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

http://www.php.net/manual/en/language.oop5.static.php

For example:

class Foo {
public static $bar = null;

public static function init() {
self::$bar = array(...);
}
}

Foo::init();

Or do it in __construct if you're going to instantiate the class.

How to assign a value from application.properties to a static variable?

Think about your problem for a second. You don't have to keep any properties from application.properties in static fields. The "workaround" suggested by Patrick is very dirty:

  • you have no idea when this static field is modified
  • you don't know which thread modifies it's value
  • any thread at any time can change value of this static field and you are screwed
  • initializing private static field that way has no sense to me

Keep in mind that when you have bean controlled by @Service annotation you delegate its creation to Spring container. Spring controls this bean lifecycle by creating only one bean that is shared across the whole application (of course you can change this behavior, but I refer to a default one here). In this case any static field has no sense - Spring makes sure that there is only one instance of UserService. And you get the error you have described, because static fields initialization happens many processor-cycles before Spring containers starts up. Here you can find more about when static fields are initialized.

Suggestion

It would be much better to do something like this:

@Service
public class UserService {
private final String svnUrl;

@Autowired
public UserService(@Value("${SVN_URL}") String svnUrl) {
this.svnUrl = svnUrl;
}
}

This approach is better for a few reasons:

  • constructor injection describes directly what values are needed to initialize the object
  • final field means that this value wont be changed after it gets initialized in a constructor call (you are thread safe)

Using @ConfigurationProperties

There is also another way to load multiple properties to a single class. It requires using prefix for all values you want to load to your configuration class. Consider following example:

@ConfigurationProperties(prefix = "test")
public class TestProperties {

private String svnUrl;

private int somePort;

// ... getters and setters
}

Spring will handle TestProperties class initialization (it will create a testProperties bean) and you can inject this object to any other bean initialized by Spring container. And here is what exemplary application.properties file look like:

test.svnUrl=https://svn.localhost.com/repo/
test.somePort=8080

Baeldung created a great post on this subject on his blog, I recommend reading it for more information.

Alternative solution

If you need somehow to use values in static context it's better to define some public class with public static final fields inside - those values will be instantiated when classloader loads this class and they wont be modified during application lifetime. The only problem is that you won't be able to load these values from Spring's application.properties file, you will have to maintain them directly in the code (or you could implement some class that loads values for these constants from properties file, but this sounds so verbose to the problem you are trying to solve).

Able to change Static variable value

The definition means that the variable will be initialized only once in a context of the class definition. Id est, for the class Test in your example it will be initialized only once no matter the number of objects you instantiate for this class.

Take also into account that initialization is not the same as changing the value of the variable later.

To ilustrate your question in comments:

class Test {
public static long staticAtr = System.currentTimeMillis();
public long nonStaticAtr = System.currentTimeMillis();

public static void main(String[] args) {

Test t1 = new Test();
Thread.sleep(100);
Test t2 = new Test();
System.out.println(t1.staticAtr);
System.out.println(t1.nonStaticAtr);
System.out.println(t2.staticAtr);
System.out.println(t2.nonStaticAtr);
}

t1 and t2 show the same staticAtr that was initialized only once at the start of the execution, while the nonStaticAtr of t1 and t2 where initialized once per instantiation and have therefor different values.

Change the value of static variable

I see 3 problems with your code.

  1. the "month" that Calendar.get(MONTH) returns is not the numeric month, but one of the constants defined in Calendar class. For example, for the month of JULY it returns 6. You should ideally migrate to using the modern classes defined in java.time, such as LocalDate, that don't behave in surprising ways.
  2. Formatting. An int does not have leading zeros. But you can use String.format to zero-pad it.
  3. Variable visibility. You are declaring a local monthBtn variable that hides the static variable with the same name.

You can get around these problems by changing the code in your method to:

    int month = LocalDate.now().getMonthValue();
monthBtn = String.format("//*[@text='%02d']", month);

Set and Get static variables values through static method in different classes in java

just call first class A to set the value for class Data then call class B to get the value inside the data.

ex:

public class A{
String nameA="Test";
public A() {
Data.setData(nameA);
}
}

public class B{
String nameB;
B() {
nameB = Data.getData();
System.out.println(nameB);

}
}

public class Data{

public static String nameD;

public static void setData(String name){ nameD = name; }

public static String getData(){ return nameD; }
}

then if u made the following, you will got ur value
new A();
new B();

Static variables initialised using static variables are not updating

Static member place will not be recalculated when changing baseUrl. You can define a custom getter function like this:

class EndPoints {
static String baseUrl = "someurl.com/";
static String get place => baseUrl + "api/v1/place";
}

With this change your code will output the place with the updated value. Also, there is a typo in your code, EndPoint.baseUrl should be EndPoints.baseUrl.

Frida - Print static variable of a class

jadx deobfuscates variable names to make code readable.
You have to use original name of member to access it.

So you need to use names "a" and "d" to access those variables:

Java.perform(function x() { 
var Test = Java.use("e.u.e.a.c.a");
console.log( Test.a.value );
console.log( Test.d.value );
});

and If we have methods with the same name as class field, then we need _ before variable name to get its value

Java.perform(function x() { 
var Test = Java.use("e.u.e.a.c.a");
console.log( Test._a.value );
console.log( Test._d.value );
});

Class (static) variables and methods

Variables declared inside the class definition, but not inside a method are class or static variables:

>>> class MyClass:
... i = 3
...
>>> MyClass.i
3

As @millerdev points out, this creates a class-level i variable, but this is distinct from any instance-level i variable, so you could have

>>> m = MyClass()
>>> m.i = 4
>>> MyClass.i, m.i
>>> (3, 4)

This is different from C++ and Java, but not so different from C#, where a static member can't be accessed using a reference to an instance.

See what the Python tutorial has to say on the subject of classes and class objects.

@Steve Johnson has already answered regarding static methods, also documented under "Built-in Functions" in the Python Library Reference.

class C:
@staticmethod
def f(arg1, arg2, ...): ...

@beidy recommends classmethods over staticmethod, as the method then receives the class type as the first argument.



Related Topics



Leave a reply



Submit