Java Access Main Class Variables
Solution 1
Pass the variables you need to your objects. You can even create a singleton class containing all the constants/configurations that your classes need.
Example given:
Constants class
public class Constants {
private static Constants instance;
private int width;
private int height;
private Constants() {
//initialize data,set some parameters...
}
public static Constants getInstance() {
if (instance == null) {
instance = new Constants();
}
return instance;
}
//getters and setters for widht and height...
}
Main class
public class Main extends JApplet {
public int width = getSize().width;
public int height = getSize().height;
public void init(){
Constants.getInstance().setWidth(width);
Constants.getInstance().setHeight(height);
Ball ball = new Ball();
}
}
Ball class
public class Ball {
int screenWidth;
int screenHeight;
public Ball(){
this.screenWidth = Constants.getInstance().getWidth();
this.screenHeight= Constants.getInstance().getHeight();
}
}
Another way can be to start the object instance with the parameters you need. Example given:
Main class
public class Main extends JApplet {
public int width = getSize().width;
public int height = getSize().height;
public void init(){
Ball ball = new Ball(width, height);
}
}
Ball class
public class Ball {
int screenWidth;
int screenHeight;
public Ball(int width, int height){
this.screenWidth = width;
this.screenHeight= height;
}
}
There are more ways to achieve this, just look out yourself and choose the one you think it would be better for your project.
Solution 2
you can access them using simply two arg constructor.
public void init(){
Ball ball = new Ball(width,height);
}
public Ball(width,height){
//access variables here from main class
}
Doug
Updated on June 04, 2022Comments
-
Doug over 1 year
I'm making a small game in which the Main class holds all the objects and variables and calls the methods within the classes themselves that do most of the work. Pretty standard. Unfortunately, that means that many of the variables I need are in the Main class where I can't access them.
For instance, as a test I wanted a ball to bounce around the screen, simple enough, but I need the dimensions of the screen, which I can get easily using the
getSize()
method in the main class. But when I create theBall
class which will bounce around, I can't access thegetSize()
method because it is in theMain
class. Is there anyway to call it?I know I can pass the variables to the
Ball
class in the constructor or for each method I need, but I want to see if there is some way I can take whichever variables I need when I need them, rather than passing it all the information whenever I make a new object.Main.class
public void Main extends JApplet { public int width = getSize().width; public int height = getSize().height; public void init(){ Ball ball = new Ball(); } }
Ball.class
public void Ball { int screenWidth; int screenHeight; public Ball(){ //Something to get variables from main class } }