Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Ok so hello all, this has really been bugging me, how can I call like other code in a different file? (All I know is simple scripting where you can call a function, is it the same?) Because say I want my user to input "north" than have it do a action like if (input.equals("north") { //blah etc, this has been bugging me forever not knowing how to do this so thanks for any help.
To clear and questions up
Basically all I want is to be able to call other code in a different class.
If they are in the same package as a given class (by making sure they have the same package packageName; line at the top of the java source files), you can simply instantiate objects that belong those classes which are defined in different files as you would any other object:
ClassFromAnotherFile obj = new ClassFromAnotherFile();
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 21 days ago.
Improve this question
I was having a look at 1.19 MCP, which I downloaded from their GitHub, when I found something strange. In the file net.minecraft.world.damagesource, the class DamageSource contains two variables, among others, that look exactly the same.
These are the declarations:
public static final DamageSource IN_FIRE = (new DamageSource("inFire")).bypassArmor().setIsFire();
public static final DamageSource ON_FIRE = (new DamageSource("onFire")).bypassArmor().setIsFire();
Does anyone know why both variables are very similar? What is the difference between them? Where is one used and where is the other one used?
I tried looking online and trying to follow the implementations of both variables, but so far no good.
Judging from the names, the one might be damage due to the player walking in fire and the other due to the player having caught fire, or in other words, being on fire.
As #Sweeper has pointed out in the comments, the distinction is required, for example, to give precise death messages.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to create JLabels in a loop and remove them at the End. So I need to give every Label its own name (Like Label[1], Label[2],...). I'm very new to Java so I can't imagine how to solve this problem. I already tried different ways but they all didn't work.
I am not sure but you can add a number of anonymous objects(in this case they will be JLabel or Label) to the Array List without naming them.(You need to use for loops). If you want to delete them, you need to use the same logic.
Let me know this works or not. If you stuck again, I can help you by writing the code. However, writing yourself will be more beneficial for you.
Have a nice day !!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
What's the difference between declaring a class inside of another class and declaring a class in a separate file?.
Literally, nested classes were added to Java for two reasons:
1. Source code clarity.
2. Name-conflict reduction.
Java actually didn't need to support doing this, but they were totally doing programmers a "solid" because I'm sure they know how messy code can get when you're in the moment of it all.
To answer your question, explicitly: The difference is that there really IS NOT a difference, it just makes code easier to read and you end up with less name-conflict mistakes.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I recently started a project in Java, that contains a class called System. This class (Luckily) contains methods for output management, so in the rare cases where I need to use the System. methods (Or the System object in general) I just reference it as java.lang.System.. I believe that this could be looked down upon, as System could be looked at as a reserved name. I currently am in the beginning stages of this program, and could change it accordingly quickly, as there are little calls to the class itself.
While it's not illegal, you don't want to do this. If I were the next person working on your code, the first thing I would do is try to remove "java.lang" from "java.lang.System" and then get miffed when it wouldn't compile.
The idea is to go toward brevity and only write what you need to write, while making sense of it all for the next person. It's more an art than a science.
You could always name it something like ProjectnamehereSystem or OutputManager or something to that effect.
I would not create something so similarly named as an important class. While everything is easy to edit, you may be able to keep up with all the changes you are making.
But when the project evolves things will get messy and complex. I would suggest naming it something else that can be easily distinguished.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
my main.java file has a length of about 1000 lines. My code is getting more and more confused, and I would like to "split" it in different parts (e.g. in one file I would have essential stuff like OnCreate, in another file I would have for instance GetHttpRequest).
I already tried to put GetHttpRequest in a different class, but is there no simpler way? (It would take a really long time to adjust the code if I used this method)
You have to use classes and methods, and optionally packages.
This will solve your problem. There's no simpler way than that.
Please do not hard-code your program. There are several patterns on how to code a program, so it is efficient, everybody can easily read and understand it. I think you also have a "GUI", assuming to this, I recommend you to use the MVC pattern. It means Model-View-Controller, so you organize your program in Packages: "model", "view", "controller" and in those packages you put the classes. For instance, you have a simple Calculator. Then you have a class in view thats called "CalculatorView", where your graphical interface is and in controller you have your "CalculatorController" that works out the things like calculations. (You call the controller from the view) and you do not need model at all.
I hope that helps you. But you will have to rewrite all your code...