I have a spring application deployed in tomcat. The application has a class (ClassWithStaticFields)with some static fields. I need to get the values of these static fields either from heap dump or from another external class(by manually placing the new class in the path of ClassWithStaticFields)
The external class when it tries to access the static field a null value is shown. I think this is because the process which started and loaded the static fields and the new class process are different.
The heap dump doesn't have any information about the class itself.
SELECT s.#staticFields FROM "com.ClassWithStaticFields" s
gives Your Query did not yield any result.
I am expecting to get/view the value of static field of a class which is deployed in tomcat. I do not want answer to suggest to put a logger/sysout/print statements.
If you need to know the static fields value in a class, first you need to know the class name. If you know the class name it would be easy then. ClassName.StaticField will give you the result.
Related
I am working on a project in NetBeans and my project is made using JavaFX,Java and mysql. I have a controller class for every display page(fxml file) that has some static variables. When I try to access those static variables anywhere within the source package I successfully get their current values but when I try doing so from the test package it returns a null value or zero value. I am writing some junit tests and I need those current values of the static variables as the tests test those values.What should I do? Any suggestion would be helpful.
To be specific I have two display pages that calculate salary of an employee -one takes input from the user such as basic salary, other allowances and it's controller class stores those values in static variables accessed in the second page controller class to calculate net salary. The variable basicsalary shows zero value in the junit test class at the same time shows the correct value when accessed in salary2 class(the display page controller). I wish to access it's current value in the test class as well.
You could copy the static values in your tests also. This will make your code more safe (for example if someone modified a static constant without wanting that, the test will fail).
That being said, you should have access to static variables in test classes as long as code compiles. Are the values assigned in another context? (like system variables, server startup...)
If I modify a static variable of a class does it get shared by previous instance of the class or only the new instance of the class.
I was just doing RMI and I have a class with static variables which I register to RMI registry. If I update the static variable by calling the RMI object it gets updated but if I update it by classname then check the value of the variable from the RMI object it gives me the old value.
If class A has a static variable s, then all instances of A in a given JVM will share that static variable. The JLS guarantees this.
Possible explanations for what you are seeing include:
You have managed to load class A multiple times in the JVM, using different classloaders. That will give you different A classes, as far as the type system is concerned, and each one will have different static variables.
You are actually talking to instances of A in a different JVM in the "by RMI" and "by classname". cases. The classes A in the different JVMs do not share static variables.
Various problems with your edit / build / deploy / run cycle.
Static variables or static method can be called without creating its class instance. Which means its values are same for all objects or any reference to that class. Does it mean we can get static values from class anywhere in our project.
In my case when user login I get all of the profile information and assign it to static variables in UserProfile.java class. Can these values be accessed anywhere in my project.
I don't want to use Shared-preferences or SQlite for storing this data.
Yes, static variables can be accessed directly from the class' name (MyClass.myVariable), as long as their access policy permits it.
Static variables are accessible directly from class without creating an object of class.
Variables you want to access must be public
you can simply access them like
YourClass.yourVariable
If those static values are public then you can access them from wherever you can access your class.
However, you cannot access classes across different modules, so to make that happen , you have to add compile project(':project_name') in your dependencies inside your gradle file.
I have a doubt about which option would be a better one in order to have a more understandable code. I have a variable that will be used just inside one method but, as this variable is a configuration variable, I think that it would be nice that this would be a global one. So I have created it on the top of the class as:
private final int VARIABLE = 5000;
But then, as this variable is used just once, the Android Studio launches a warning that says "Field can be converted to a local variable". Do you think that is better to keep this variable as a global one (in order to be more clear for the future) or it is better to set it as a local variable?
Thanks in advance!
Keep all your variables which are not going to change, for example base url strings etc in a separate java file in a separate package like Utils. Place all your variable there and make them static so that you can directly access them by using the class name.
I just want to see what you guys might think the difference (in terms of usage, efficiency, or even good practice) with regard to android development.
If I use a static variable in one of my base activity (so its single instance and be access everywhere), as opposed to using a non static variable in my application subclass (which is a single application class for all the activities).
Both will achieve the same end results if you try to use a global variable.
I was using the static one then I moved to use the application subclass (in case you guys wonder "what is it I am using for", I wanted to play background music and control it from everywhere and I don't wish to use service for certain reasons).
Any help clarifying the best way to ?
It depends on use also, suppose if you are using
android:process
for some reason in your Activity or anything else in your Manifest file your static value will be reset and you will get the initial value assigned to static variable. In that case you can use SharedPreference or Application class.
Because if you use android:process for any particular Activity then that Activity will run in another process and as we know that in Android every Application runs in its own process.
Other than this I don't see there is much issue using static. But, personally I would prefer Application class as Android has it for global variables.
During the execution of a program, each variable has its own time within which it can be accessed. This is called the lifetime of the variable.
Instance variables:
Instance variables are class members. Every time you create an object from a class, a brand new copy of these instance variables is created for this object. Actually, creating an object from a class means, partially, creating copies of instance variables for that object. So each object has its own copy of instance variables which exist as long as the object they belong to exists. The values of these variables constitute what we call: the state of the object.
Static variables:
Static variables are also members of a class but can't belong to any object created from that class. So created objects from the class don't get their own copies of static variables. Therefore static variables are created only when the class is loaded at runtime. The existence of static variables is dependent only on the class itself. As a result, a static variable exists as long as its class exists.
One of main difference between these two variable is that when you are calling System.gc(); your instance variable is set to null while static variable will never set to null by calling gc.