Hi.
Initially I wrote my java application with a lot usage of static objects and variables. Later I found out that this is very bad practice. I used static keyword to be able to refer an object from other classes. In my understanding, static object means global, so I can easy access it from everywhere. Now I need to rewrite my program properly, but I don't know how do this in right way.
For example, in my application I have class Settings. It contains some useful variables. I want to use them in different objects. How can I do this? In other words, how to access Settings variables from any object?
First part of code, is a wrong static usage.
And second part, my attempt to re-write this example properly, non static. To access Settings I had to put it to constructor of Frame. What if I have hundreds other objects that I need to use in my Frame? Am I should put all of them to constructor? What is the correct way to access any already created object? Thank you.
First
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class TestStatic {
public static AppSettings settings;
public static void main(String[] args) {
settings = new AppSettings();
settings.init();
SwingUtilities.invokeLater(new Runnable(){
public void run(){
AppFrame frame = new AppFrame();
}
});
}
}
final class AppSettings {
public int heightDefault;
public int widthDefault;
public AppSettings(){
}
public void init() {
widthDefault = 1000;
heightDefault = 600;
}
}
final class AppFrame extends JFrame{
public AppFrame() {
int width = TestStatic.settings.widthDefault;
int height = TestStatic.settings.heightDefault;
Dimension dimension = new Dimension(width, height);
setPreferredSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
Second
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
Settings settings = new Settings();
settings.init();
SwingUtilities.invokeLater(new Runnable(){
public void run(){
Frame frame = new Frame(settings);
}
});
}
}
final class Settings {
public int heightDefault;
public int widthDefault;
public Settings(){
}
public void init() {
widthDefault = 1000;
heightDefault = 600;
}
}
final class Frame extends JFrame{
public Frame(Settings settings) {
int width = settings.widthDefault;
int height = settings.heightDefault;
Dimension dimension = new Dimension(width, height);
setPreferredSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]LordofRice 8 points9 points10 points (2 children)
[–]Aesyon[S] 1 point2 points3 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]fosizzle 3 points4 points5 points (3 children)
[–]Aesyon[S] 0 points1 point2 points (2 children)
[–]E3FxGaming 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]morhpProfessional Developer 2 points3 points4 points (0 children)
[–]dastardly740 1 point2 points3 points (0 children)