I have an integer(levelstatus) in class A(LevelSelectScene) and I want to change it in class B(GameScene), is it possible?
here is my code:
(Code in GameScene)
public class levelComplete()
{
levelSelectScene.getInstance.levelstatus=1;
}
LevelSelectScene has an public integer levelstatus.
and after an event happens, levelComplete will trigger and will change the value of levelstatus from null to 1.
Yes.
Make your levelstatus variable as static.
Because I guess you need to change that variable in each level class.
That means,you are wanting to access that variable through out the whole Game(All levels).
And also,Declare that variable in a Util class(proposing a name LevelUtil),Since it wont attach to a single level.
It is possible to change the value any where in the project(that is in any class) since the variable is declared as public levelstatus.
If you want to change the value of the variable in many places and the new value should be the modification of previously changed value, the you should declare the variable as public static levelstatus.
I believe it is more appropriate if you look into SharedPrefences of the Android API.
Check it here.
It could be used across the whole project of yours and it is handy in those kind of cases.
You can access your variable as a public static.
Definition:
public static Boolean isPremium;
Access (do not forget import) :
your_class.isPremium
Related
I would like every object of a class be able to access a class variable but don't want to get modified outside class.
For example the way Array.length is used.
But would also like to have a method in class which conditionally modifies this variable.
Basically this usage is for a resizing object array implementation, where length of object array can change dynamically.
There seem to be a few confusing points here.
I would like every object of a class be able to access a class variable but don't want to get modified outside class. For example the way Array.length is used.
Array.length is (effectively) a final field - an array's length in Java cannot change, so it cannot be modified from inside or outside the class.
But would also like to have a method in class which conditionally modifies this variable.
If a variable is final, as the length field in array, then its value cannot be modified at all, by anything.
You're not going to get this level of control from a field directly. Instead you should follow the standard getter pattern - make the field private and non-final, provide a public method to get its value (getLength() for example) and then set it as you need to within that class. This gives you the flexibility to change the value from within the class as you need to, while still allowing the value to be read from anywhere else.
Array.length is marked final and hence cannot be modified after the constructor is returned. I don't know what you're talking about there.
I guess, you want a private field with public getters and private setters?
private int someField = 0;
public int getSomeField() { return someField; }
private void setSomeField(int newValue) {
if (someCondition) {
someField = newValue;
}
}
Since both someField and setSomeField are private, no code outside of this class can modify the value of someField.
Code outside can only get the value of someField.
Code inside the class can conditionally set the value of someField by calling setSomeField.
Requirements met!
I think what I was trying to do could be accomplished by ArrayList implementation. Thanks for your replies.
I am asking the user to input a value and then I am assigning the same to an attribute but, I want that change to be global. I searched on internet but yet I am not able to find the working solution for it. By the way I m using Java.
Thanx in advance.....
Java has class members for that. Also known as static fields.
This question has already been extensively covered. If you are looking on how to make a variable global/accessible from outside a class, check here.
However, if you are looking to make a global variable that can be accessed in and ONLY in a class, you can use the private static Keyword. Here is an example:
class myClass {
private static int myGlobalVariable; //Can only be accessed from methods in myClass
void changeMyVariable (int value) {
// Data validation here if needed
this.myGlobalVariable = value;
}
}
Is it possible to declare a class level variable from within a method?
I'm simply trying to create a static counter, but I only want to create it if needed. The class calling it should live longer than the function itself.
My need for this is rare, but I'm interested if it would work.
Not it is not possible to declare class level field, unless you're doing some metaprogramming stuff like insane bytecode level modification that wouldn't pay for itself. All declaration is done at compile time.
"Is it possible to declare a class level variable from within a method?"
If literally - yes :) it is possible with method-local classes :
void method() {
class LocalClass {
private Object variable;
}
}
'static ... when needed' is a contradiction in terms. The answer is 'no'.
I've read many large projects in OOP, and I notice that a lot of them use this.[variable], [ClassName].[staticVariable]. For example:
public class ABC {
private float mX;
public static float y;
public float getX() {
return this.mX;
}
public float doSomethingWithY() {
return ABC.y;
}
}
And even with Eclipse auto-generated Getters & Setters feature, it also comes with this.[variable], although it's unnecessary, because no local variable is declared there.
Is there any advantage when using these notations, or it's just a code style?
EDIT so some people don't misunderstand. I know what this and [ClassName].[staticVariable] stand for. But in this case, it's unnecessary. The question is: Even if it's unnecessary, why do guru coders still add it? When they need to update/fix a huge project, will there be any advantage and disadvantage?
Basically with this, you KNOW for sure that you are working with a class attribute, not with a variable created inside the method or maybe received as a parameter.
And also, it helps in case you have a local var with the same name.
And the final reason: readability.
It's necessary in some circumstances, for example this. is required when you need to use a member variable rather than a local method parameter of the same name.
It's also necessary for static variables where you need to be specific which class you want to get the static variable from (many classes could define static variables with the same name).
Apart from the necessary cases, it's really a matter of coding style. My recommendation is to use it whenever it helps to resolve potential ambiguity.
In complicated methods, it's sometimes nice to make a distinction between instance variables in this class, and local variables in a particular function. This distinction is immediately obvious when you use "this."
For small pieces of code it doesn't matter but sometimes this can happen:
public float getX() {
....
int mX = someFunc()
...
return mX;
}
In this case, the local value is returned instead of the member variable.
You normally want to be explicit and say this.mX. However, you shouldn't have huge functions anyway.
this.? '?' is a member variable, this is a reference to the current object.
see this
Its syntax,if you want to access instance variable of a class use the (reference of the object).(instance variable name) .Like
A a= new A();// for non static class,this for the current object
a.(instance variable name)
// for non static class do the same but use (class name).variable name
A variable is declared as static to get the latest and single copy of its value; it means the value is going to be changed somewhere. But why should the same variable be declared as final, which will not allow the variable to be changed else where (constant value)?
static so that the variable or method can be accessed without creating a class instance, and there is only one variable for the class instead of one for each instance.
A final class cannot be extended. A final variable cannot have its value changed, it behaves as a constant. And a final method cannot be over-ridden.
The minute a variable is defined as final, it should probably not be referred to as "variable", since it no longer "varies" :)
A static variable is not tied to any particular instance of a class -- it is only tied to the class itself and only from a scoping standpoint.
So there you are -- a static and final variable is actually a value that is not tied to any particular instance of class and does not vary. It is a constant value, to be referenced from anywhere in your Java code.
At some point, when you should decide to change the value of this constant, it only takes one change to propagate this change correctly to all other classes that use this constant.
A variable declared as static means that its value is shared by all instances of this class. Declaring a variable as final gives a slightly better performance and makes your code better readable.
local variables are on the stack and are not static.
You can have a static field which may or may not be final. You would make the field final if it is not going to change.
static fields can be modified (e.g. public static fields can be modified by any class). static final fields cannot be modified after initialization.
Like you mention yourself, this is done to create constants. You create a single field to hold a value with a specific meaning. This way you don't have to declare that value everywhere, but instead you can reference the static.
Static has nothing to do with getting the latest and single copy unless "single copy" here means one and the same value for all the instances of a class (however, I think you may be confusing it with volatile). Static means class variable. You make it final when you want that to be a constant (that's actually the way Java constants are declared: static final).
static final is used in Java to express constants. Static is used to express class variables, so that there is no need to instantiate an object for that class in order to access that variable.
Final methods can't be overriden and final variables can only be initialised once.
If you only use the static keyword, that value will not be a constant as it can be initialised again.
May be to provide something similar to constants.
Variables should be declared as fields only if they’re required for use in more than one method of the class or if the program should save their values between calls to the class’s methods.
for example user.lastName lastName should be field because it is needed during object lifecycle
Variables should be declared as static only if they’re not required for use in more than one method of the class or if the program should not save their values between calls to the class’s methods.
for example Math.max(num1,num2) Im not intristed in num1 and num2 after compleating this operation
Final stops any classes inheriting from it
You create static final variable to make its value accessible without instantiating an object.
E.G.:
public class MyClass
{
public static final String endpoint= "http://localhost:8080/myClass":
/* ...*/
}
Then you can access to the data using this line:
MyClass.endpoint