Forums Register Login
explanation of piece of launcher class needed
Hi - thanks in advance!

In this tiny run program to run a separate class,
public class CashRegisterLauncher {
public static void main(String[] args) {
CashRegister register = new CashRegister();

what exactly does in JAVA english speak (hope you know what I mean by this!)



such as "the class file launcher takes the [newly] instantiated instance of the register object and then triggers the running of the other class file containing the run (or old main method) along with all expected arguments."

I guess I'm wondering how the object gets passed somewhere else and exactly how the run main method gets triggered.
Campbell Ritchie was helping me with this before on the Java in general area
thanks again
(1 like)
When you type this at the shell:

java CashRegisterLauncher

it looks for a main method in that class to run. That's just the way Java is written.


register is a reference that points to an object. This object has a "run" method that takes a String[]. "register.run(args)" says "execute the method run on the register object, passing args to it."

This is really no different from any other method call.


myString is a reference to some object.

charAt is the method you want to call.

2 is the argument you pass in

In a similar fashion:

register is a reference to some object

run is a method you want to call

args is the argument you pass in.


Jessica Stensrud wrote:
what exactly does in JAVA english speak (hope you know what I mean by this!)



when you say
java CashRegisterLauncher

Java first looks for the class "CashRegisterLauncher"
then looks for a properly formed public/static main method
now it runs the main method.

The first line of your main method declares a reference named register, and it points to a new CashRegister object (which the jvm instantiates for you and places on the heap)
The second line, you're saying to the jvm "please take the object that register points to, and run the "run" method on it, with args as the parameters"
so now the object on the heap runs the run method, using the "args" parameter you passed in.

clear as mud?
Just like Knute said
Because I typed the response without reading everything in depth.
Do not threaten THIS beaver! Not even with this tiny ad:

This thread has been viewed 485 times.

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2018 07:21:20.