Getting static values from class in android - java

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.

Related

Get the static variable values of a class

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.

Is it bad OOP practise to declare global class variables in my main class?

I need to access an ArrayList through multiple functions in my main Java class file (the class file that contains main()).
To save myself having to pass the array list between functions, can I declare it outside of the methods? Is this bad practise? I understand this is okay with class files that are initialised as objects (private instance variables for example) - but what about the Main class file?
EDIT: It will be the only class variable declared and available to all functions in that class.
If you declare it as private it's okay. What it would be a very bad practice is to declare it as public.
You are good as long as you keep it private, since that way you'll make sure it is accessible only from within that class. On the other hand, if you do require it to be accessed from other classes, use getter setter methods while still keeping it private within the class itself.

Field to local or global variable

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.

static variables Vs application variables

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.

Safe and efficient method to adapt static class for modification

I'm adapting a simulation written in Java. My limited background is all in C++.
The current implementation of the simulation relies on a class called Parameters. An instance of class Simulation references attributes of Parameters, which I don't think is ever instantiated. The Parameters class has a structure like
public class Parameters {
public static int day = 0;
public static final int endDay = 365;
....
public static int getDate() {
return day;
}
}
In an instance of Simulation, there are references to, e.g., Parameters.day.
Currently, all the attributes of Parameters are hard-coded. I need to be able to change some of them with command-line arguments. For example, I'd like to be able to set a different endDay using a Parameters::setEndDay(int customDay) sort of function.
My first thought was to create an instance (Parameters parameters = new Parameters()) and completely rewrite the Parameters class so that all its attributes are private and only accessible through accessor functions. I'm concerned that this approach is not very efficient. Thus far, I have tried a hybrid approach in which I create an instance of the Parameters class, which I then pass to an instance of Simulation while still occasionally referencing Parameters.day (something I don't need to change).
One of the problems is that I don't have a good sense of class privileges in Java.
Suggestions appreciated.
If you make all of them non-final, then you can just set them directly from your command-line arguments before instantiating the Simulation class:
// read command-line arguments
Parameters.endDay = 123; // this will change all reference to Parameters.endDay
new Simulation(...)
create static method setEndDay(int customDay) in Parameters class. And you can change value without accesing with class not object: Parameter.setEndDay(10). Note that endDay variable should be non final.
Accessing static member variables vs instantiating an object
Essentially this is a choice between global data or local data. A class variable (keyword static) exist in one place for your entire application. E.g. you cannot have two parametrizations running simultaneously in the same application (although you can run two applications in parallell or sequentially). Using instantiated objects you can have several Parameter objects containing different values.
Accessor methods vs accessible member variables
There are some controversy around this. Schoolbooks usually state that all data should be encapsulated using accessor methods to protect the objects internal state. It is however as you state slightly less efficient and there are cases where making member variables directly accessible is considered good practice.
Java Access Modifiers
Java supports four different access modifiers for methods and member variables.
Private. Only accessible from within the class itself.
Protected. Can be accessed from the same package and a subclass existing in any package.
Default (no keyword). Only accessible by classes from the same package.
Public. Accessible from all classes.

Categories

Resources