Passing Parameters in JavaFx - java

I have implemented a GUI using javafx in a class called "Gui" which extends apllication. I have a seperate class which handles the logic called "Logic". I want to pass an instance of "Logic" class to to "Gui" class. Is there anyway that I can create an instance of "Gui" class before calling "Application.launch()" in main method?

Not easily, and this is almost always the wrong way to approach this. In particular, you don't even know that the main(...) method will be invoked: in Java 8 a JavaFX application is launched by the JVM without (necessarily) calling main(...) at all.
You should really consider the start(...) method in your Application subclass as the equivalent of the main(...) method in a regular Java application; in other words, think of this as your application entry point and create the Logic instance there.

Related

In java, do I always need a Main class?

I know that I'll need a main method, but can that main method be in a different class other than the Main class?
Not all Java applications require a main method.
Java can also be used to create web applications, for instance, which don't require main methods to run.
The answer to your question depends on what exactly you mean. Do you mean a class with the name 'Main'? Then, no, there is no requirement for this at all.
The only requirement that Java has, is that the signature of the method is correct. the main method must:
be public
be main
be static
have returntype void
accept an array of Strings as (only) parameter
It's easier to add it in the public class in a file, but not mandatory. The name of the class it is in, is entirely up to you, though many will choose a name like 'Main' or 'Open', simply to more easily find it.
If you want to be able to run your application, by simple double-clicking the .jar file, you'll need to point to the class that contains the main method (to use: your application might contain a lot of main classes, used for internal testing, but only one can be used to start the actual application) in the manifest file: Manifest files
Prior to Java 7, it was possible to run a desktop application without a main method, by (ab)using an instantiation block, but this was removed as of Java 7, because this is not what the instantiation block was intended for.
It's not necessary to define yout main method in a main class. You can place your main method wherever you want, as long the syntax i correct :
public static void main (String[] args){
//...
}
You absolutely don't.
The method itself can be placed whereever you want it to be, there is no limitation.
However, I personally would recommend putting it in a class which at least contains something like "Main", because when others look at your code, and they are not using an IDE which supports jumping to the main method, people usually have an easier time finding your starting point.
However, that is just for sake of readability, and as I said, jumping to main is/should be usually a widespread supported feature
Yes, the Main method is required to run a function although a java class can be without the Main method. Though, it won't run...

Java - Hiding update functions in project

I'm creating simple game engine in Java and I've got some packages like a:
Game
Input
Time
Graphics
Each package handles a lot of classes, most of them have (And should have) public access. Let's focus on one most important class called MouseInput.
MouseInput class handles ONLY public static methods like a getMousePosition(MouseAxis axis) {...} but it also handles some methods like a updateMousePosition() {...}.
And now I want to make this method (updateMousePosition()) callable ONLY by GameBase class that is inside Game package.
P.s. I don't want to put all those classes in one package! I want to separate them to don't make my project messy.
2th P.s. All those methods that I want to make callable only by GameBase are static.
It is possible to restrict who can call even public method by checking the call stack. This adds some code to the method receiving the call but any "wrong" calls can be rejected.

Generally what is put in the main method and what isn't? - java

Beginner just starting out
.can i write entirety of code outside of main and still run? generally i'm asking what is "main" used for?
In theory you can just code everything in main and in will work. However to make your code structured you probably want to code outside of main and call it from main.
The main Method is the start point for the VM of Java. So if you want to run your application, there has to be a main method. Not only Java has this concept, also other OOP-Programminglanguages like C,C++,C#. You can write code outside the main. but remember that the code can only be accessed if there is a way to it from the main Method, example functions.
The main method is where your system is able to call and start the program up. The main method is needed if you wish to run your program and make use of your other classes.
You can create your program inside the main method if you wish, but as your programs become more sophisticated, you are going to want to create other classes that are initialized and used in your main method.
For example, if you make two classes called "Suv" "Sedan", and make one more class called "Mechanic" - the class that will hold your main - you can initialize Suv and Sedan and you can call the methods you made in Suv and Sedan to do things like change the engine, or to change the color, etc.
If you are just learning java, then don't worry about it too much and just write your code in your main method.
Main is the entry point of any java application. The Java Virtual Machine starts up by loading a specified class and then invoking the method main in this specified class.
You can write your code in main method, or somewhere else and call that in the main method in the order in which you want to execute it.
The method main must be declared public, static, and void. Following is the reason for it:
public:- main is marked public so that it can be accessed from anywhere outside the scope of the class or broadly we can say outside the scope of project.
static:- It is marked static so that it can be called without any instance of the class. Without declaring main method static, your program will compile successfully but throw an error at run time.
void:- The keyword void is used with main to tell the JVM that the main method does not return any value. It is solely for the initiation of the application not for returning anything.
Following is the valid declaration of the main method:
public static void main(String[] args)
public static void main(String... args)

Where about should my main class be created in a project?

The problem is where a class should be created in my code. An example is I have a UI class and a main logic class that controls other objects.
Should the main logic class create the UI object, or should the UI object create the instance of the main logic class?
An explanation of which method is best and why would be ideal.
Thanks.
Neither.
I think controller classes should instantiate the UI.
Whether you have a driver that contains the main, or the controller owns it, is immaterial.
If you change UI, your app should still work fine. Don't put the main in the UI classes.

Questions regarding Extending JFrame in a class?

Okay, I'm NOT a Java noob, it just so happens that I've forgotten a tad bit about core Java while I was learning more fun stuff like MySQL, Servlets, Java EE, JDBC etc etc; so don't frame your answers as if I were a beginner. Now the question.....
I'm writing a class (lets say ThisIsAJFrameExtendingClass) which extends JFrame, and to minimize confusion with my overall project, I also want to park some other utility methods in ThisIsAJFrameExtendingClass. I intend this class (ThisIsAJFrameExtendingClass) to seek certain inputs from the user following which; commit suicide (ie dispose()). So, my question is, how can I independently use the utility methods inside the class without any JFrame popping up on the user screen.
I'ld like a solution with the help of multiple constructors inside the ThisIsAJFrameExtendingClass class, where, invoking the argument-less constructor return JFrame and the second constructor with a boolean argument gives access to the utility methods.
[UPDATE]
Ohh.... I just had a thought, the utility method has a return type of ArrayList so, assuming the utility method is called utilMethod() then:
ArrayList<String> pring = new ThisIsAJFrameExtendingClass().utilMethod();
will the above code output any JFrame?
You could make the utility methods static, in which case they can be invoked using ThisIsAJFrameExtendingClass.<method name> without creating an instance.
The stuff about constructors doesn't really make sense to me. A class's constructor always returns an instance of that class. It can't return "something else" because of a parameter you pass in.
[Edited to respond to the question's Update]:
new ThisIsAJFrameExtendingClass() will create an instance of your class, running its constructor (and the default constructor of all superclasses). This may allocate other resources (such as other Swing components or whatever) that each constructor in the inheritance tree requires. So a JFrame is created, but if you only call utilMethod() and never hang on to the reference to the frame, it will be garbage collected and its resources freed at some point in the future.
Creating a JFrame instance to call a single utility method on it isn't a particularly efficient way to go about things, but it won't cause any problems. (As Chad says, by default a JFrame isn't visible, so users won't see anything if you're using it in "util" mode).
As to returning an ArrayList, as a general rule when using collections, you should return the highest level interface that makes sense rather than a concrete class. So in this case, consider returning List<String> or even Collection<String>.
I have a lot of trouble getting behind your concept, which sounds a bit confused to me. At the very least, it sounds like horrible design. But I do have some suggestions:
You can make those utility methods static, then you won't need to instantiate your class at all to use them. This would be the simplest case.
You could pack your utility methods inside a static inner class of your frame, which essentially gets you around the requirement to only have one class per file.
Finally, do you just want the JFrame to disappear once the user is done with it, or do you want to terminate the application? dispose() will do only the former, your app will continue to run as a kind of headless zombie process.
Okay let's assume the methods you need aren't static.
In that case, remember the JFrame won't show up unless you call setVisible(true); So just make sure you never show the frame, and you can use whatever functions you want without it annoying the user.
Or you could design it properly and break out the utility methods into a separate class...

Categories

Resources