In this code, I am trying to make global instance of class so any methods can use it:
public static void clip(){
while(!Display.isCloseRequested()){
glClear(GL_COLOR_BUFFER_BIT);
if(character.gravity)character.y++; //there is an error, it says "Variable character does not exist"
Display.update();
Display.sync(70);
}
}
public static void initGame(){
entity character = new entity(); // I want to use this in method clip()
character.create(true, "box", true);
}
I've searched google, but on my question "Make instances of class global" found nothing.
Thanks for help.
There is no such thing as a "global variable" in java.
However, you can declare character
as a class variable by declaring:
entity character = new entity();
outside of methods, only on the class scope.
Something like that:
class MyClass {
private static entity character = new entity(); //class variable!
public static void clip(){
while(!Display.isCloseRequested()){
glClear(GL_COLOR_BUFFER_BIT);
if(character.gravity)character.y++;
Display.update();
Display.sync(70);
}
}
public static void initGame(){
character = new entity(); // it will 'reset' game and bind a new object to the class variable `character`.
character.create(true, "box", true);
}
}
More info on class variables can be in the official documentation.
As a side node, a class should not be named entity
, since java has a strong convention that classes names should always start with upper case letter, you should rename this class to Entity
.
If you want one global object for a class, you can make a Singleton as follows.
class MyClass {
private MyClass instance = null;
public static MyClass getInstance() {
if (instance == null) {
instance = new MyClass();
}
return instance;
}
}
Now you can access the "global" instance:
MyClass global = MyClass.getInstance();
To make an instance of a particular class global to your program, one solution is the Singleton pattern. It works in the following way:
class Entity {
private static Entity self = new Entity();
public static Entity get() { return self; }
}
You can then use the unique instance of class Entity from wherever in the code by:
Entity.get()
Create singleton instance of your global class, and add static instance getter for it. Later on in any piece of your code you will be able to get your instance and call any public method of its class. Alternativly if you dont need singletons, simply use static method.
In Java, all objects should either be variables or members of a class, even though they are static members. C-style globals do not exist.
So in your case, to make the character
accessible from everywhere, you should write:
public static entity CHARACTER = null;
public static void initGame(){
CHARACTER = new entity(); // I want to use this in method clip()
CHARACTER .create(true, "box", true);
}
And, provided the code above is from the class MyClass
, and the code below from another class:
public static void clip(){
while(!Display.isCloseRequested()){
glClear(GL_COLOR_BUFFER_BIT);
if(MyClass.CHARACTER.gravity) MyClass.CHARACTER.y++;
Display.update();
Display.sync(70);
}
}
This being said, this code (its design actually) should be improved as it is absolutely not object oriented. As this is another question I won't elaborate further here but suggest that you investigate further in OOP standards and best practices.