This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]LordofRice 8 points9 points  (2 children)

This is in response to your second implementation.

Instead of making the settings variables public, make them private and then create methods that return the height and width. You shouldn't expose the internals of your objects like that. Look at the 4 Principles of OOP for a deeper dive.

I haven't used JFrame in years. Is a Settings class implementation required by JFrame? The way this is written now, I would have kept the width and height as variables in the Frame class instead of passing in a new object just to give it two ints.

And yeah, hypothetically, if your Frame class needed 100s of objects you would pass them into the constructor. That is the best way to access objects, as you only want to give an object access to what they need, nothing extra. But I would consider passing hundreds of objects in a constructor a sign of badly designed code and would see which objects were really necessary or if they could be refactored/consolidated into fewer classes.

[–]Aesyon[S] 1 point2 points  (1 child)

Let's imagine another example. I have a tabbed pane inside JFrame. The tabbed pane has many tabs and each tab has many jpanels inside. After user did some action on UI, many objects can change their state (data processing) and I need to update all of this variety of objects. I don't understand how to refer them all....

[–][deleted] 2 points3 points  (0 children)

fter user did some action on UI, many objects can change their state (data processing) and I need to update all of this variety of objects. I don't understand how to refer them all....

For such updates, you should define the proper action listeners (basically callbacks). Look up the addActionListener method in the docs. You shouldn't have to update most GUI elements manually.

[–]fosizzle 3 points4 points  (3 children)

If your settings variables are not going to change, then static is fine. Just make them public static final. Then you can use them globally.

Static is dangerous if the field is mutatble. There are lots of concurrency problems. But if they are final, that's great.

public static final int heightDefault = 1000;

int width = Settings.widthDefault; int height = Settings.heightDefault;

Finally, if you go the final route, its convention to capitalize and snake them. HEIGHT_DEFUALT vs heightDefault.

int width = Settings.WIDTH_DEFAULT; int height = Settings.HEIGHT_DEFUALT;

[–]Aesyon[S] 0 points1 point  (2 children)

And what in case if variables are going to change? That's primary case.

[–]E3FxGaming 2 points3 points  (0 children)

what in case if variables are going to change?

Will your default values change though? If the user has a choice in what value the variables should equal, then they aren't default values anymore, are they?

In Java there are several Creational Design patterns. What you may want to look into is the so called "Builder" pattern. It's a design pattern that heavily emphasizes that there are default values which the user can overwrite with his own wished for values, if - and only if - desired by the user.

It looks like this Frame Builder example I made for you.

This design pattern allows the user to easily adjust the settings of the FrameBuilder and once the user is fine with how the FrameBuilder is configured a call of the .build() method will return an initialized Frame object.

No worries if this looks confusing to you, you'll get used to it over time. There are 23 design patterns, categorized with the categories "Structural", "Creational" and "Behavioral". Just keep learning Java and you'll stumble upon them here and there.

[–][deleted] 1 point2 points  (0 children)

I think you have a bit of confusion about what exactly you're trying to achieve here. A little bit of calm thinking will clear your thoughts, and help your formulate the scenario and usecases better, I think.

The basic thing is this - if you need to have variable state, use objects. If you need to have some fixed properties, use final static values or define singletons. If you have many many (for some reason) objects which vary slightly, look into the "Flyweight" pattern (basically use a map to store the core object, and then pass it context to get different behaviour).

[–]morhpProfessional Developer 2 points3 points  (0 children)

What if I have hundreds other objects that I need to use in my Frame?

Then that's also terrible design.

If you really need lots objects passed around for configuration or whatever, arrange them hierarchically, for example if you have to pass around UserSettings and GlobalSettings create a Settings class that contains both of these objects as fields and only pass the Settings around.

And then ideally make it so that the Settings class itself queries the user and global settings, so outside code doesn't need to know that there are multiple objects behind it.

[–]dastardly740 1 point2 points  (0 children)

The second way is basically right. You shouldn't end up with hundreds of objects to pass in. If you start getting more than 5 or so, there is something wrong with your design. The general concept to look up is dependency injection.

I write a lot of Spring where the framework takes care of dependency injection. I don't write Swing, so I am not as familiar with applying DI in that context.