• Post Reply Bookmark Topic Watch Topic
  • New Topic

explanation of piece of launcher class needed  RSS feed

 
Jessica Stensrud
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
register.run(args);
}
}

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

register.run(args);

do?

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
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.run(args)

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."
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is really no different from any other method call.

myString.charAt(2);

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.

 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jessica Stensrud wrote:
what exactly does in JAVA english speak (hope you know what I mean by this!)

register.run(args);

do?


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?
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just like Knute said
Because I typed the response without reading everything in depth.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!