wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How does a Java program run? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does a Java program run?" Watch "How does a Java program run?" New topic
Author

How does a Java program run?

Jane Hall
Greenhorn

Joined: Jul 31, 2012
Posts: 12
I don't mean what to click to make it run. I'd like to know what happens in the program when I run it.

I'm used to (simple!) Matlab, where everything is run in the order you've written it in. But in Java, you have different methods. From what I've learnt, you call that method from within another method.. for example,



I suppose my question is (assuming there's a main method in the program), why do you have to call doSomething() inside another method? Why doesn't Java look at init() and then doSomething()? If it does look at them both, in order, why doesn't doSomething() get called twice (once within init() and once on its own)? - How does Java run a class?

I hope that made sense!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

Declaring a method isn't the same as telling the runtime to execute it. It just means it's available to be executed.

There's only one method that gets called automatically. That's the main method. Everything else follows from there - your main method can call methods, which can call more methods, and so on. Nothing gets executed unless you've told the runtime to do it.

So in your example you've declared two methods. But neither will be executed unless you've got a main method that calls them directly or causes them to be called. For example, your main method might call init(). Then that calls doSomething().
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Everything is run in the order you’ve written it in.
Like following a road map. Only Matlab is written in American and Java in English. Go to the States and all the roads are straight. Come to England (particularly the North Riding or Devon and Cornwall) and try to find a road which doesn’t wind confusingly.

You tell the JVM to start with the java command, and a class to start with, so it looks for a main method in that class. That is what you wrote, and that is the first code it executes.
In that main method, there is an instruction to create an object, so it does that. that is what you wrote that it found second,
Then to call a method on that object, which is what it does next. If there are instructions to call other methods, those are called.
If the code you posted is in a class Foo, then you might have new Foo().init(); in the main method. So it looks for the Foo class, creates an object, and calls its init() method.
When you get to the init() method, forgetting the spelling errors in your post, it calls the doSomething method. Etc., etc., etc.

Download an IDE, write a little application with three classes, and rather than executing it, debug it. Go through it with the step in, step across and step return commands (on Eclipse f6, f, 7 and f8), and the IDE will allow you to watch what is happening.

And if nobody has said so already, welcome to the Ranch
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

Just to be pedantic, when you type

it really looks for the

method. Your class can legally have a private main method, a main method that returns an argument, a main method that takes an int and a float...

but ONLY the main method with the signature above will be called automatically. after that, you can step through things line by line, and logically determine what will get called next.

And there is a good reason to not call doSomething() automatically. a simple pseudocode example:


So here, depending on what the user enters, I only want one of three methods to run. I have to define all three methods ahead of time, since I don't know what I will want. But each time the program runs, only one will be called.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jane Hall
Greenhorn

Joined: Jul 31, 2012
Posts: 12
Thanks for your replies! I'd just like to check I understand right.

When you run a Java program, it looks at main, and executes what it finds there.

If you have something like Hangman hangmanGame = new Hangman(); then main calls the constructor Hangman().

The constructor would be the only thing to run in the class, so any methods you want to call must be in it. (Or must be called in a different method which is itself called in the constructor).

Is that right?

Can you call methods using other means? For example, in my constructor, I've written button.addActionListener(this); Does that automatically call the actionPerformed(ActionEvent e) method? Or is something else going on?

I'll have a look at debugging - thanks for the suggestion.


Jane Hall
Greenhorn

Joined: Jul 31, 2012
Posts: 12
Also, what does



do? (I tried searching for it, but ended up with various ways of getting Java. I also tried get command Java, but no luck. Is get a command?)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

Jane Hall wrote:Also, what does
do?

I think that was just intended as pseudo-code. That is, "something happens here, that will return a gender, but we're going to omit the details".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
What are you using that ActionListener for? Is it for your noughts‑and‑crosses? [Tangent: I shall get chucked off this North American website for not saying tic‑tac‑toe.] Go and find some of my rants about addActionListener(this) in the Swing forum. You will probably have to go back to 2006 because I avoided repeating myself by quoting them since then.
I suggest you need a Listener object which links to OX and to the button.
  • Do you know how to use arrays?
  • Do you know how to increment an index?
  • Do you know about the length field of an array?
  • Can you use the % operator?
  • Do you know how to use those four things together to run round an array repeatedly?
  • Can you put that lot together to get OXOXOXOXOXOXO in sequence? Do that lot, and I shall then make some suggestions for how to get OXOXOXOXOXOX out of your listener class. I won’t try to tell you everything at once.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37884
        
      22
    Jane Hall wrote: . . . looks at main, and executes what it finds there.

    If you have something like Hangman hangmanGame = new Hangman(); then main calls the constructor Hangman().
    Yes
    The constructor would be the only thing to run in the class, so any methods you want to call must be in it. (Or must be called in a different method which is itself called in the constructor).
    No. You would call another method. That method would call your other methods in order. You might have another line like…and the start method contains all the instructions to get the game going.
    . . . Can you call methods using other means?
    Yes
    For example, in my constructor, I've written button.addActionListener(this); Does that automatically call the actionPerformed(ActionEvent e) method? . . .
    No. In a GUI application, actionPerformed methods are called by the JVM (or something like that) when the operating system sends an Event. That means, when you click the button, the OS sends an event, which passes through all objects at that location on screen until it finds something which is listening for it. When it finds whatever is listening, then the JVM invokes actionPerformed (or something vaguely along those lines). In fact there are hundreds of methods which can be called when events occur, but most events simply disappear into cyber‑limbo (officially called garbage collection) without starting any methods off.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: How does a Java program run?
     
    Similar Threads
    Class Object and Hierarchy
    kb6 self test question and general doubt regarding when overriding rules apply?
    Initialization
    Tomcat, MySQL,JSP/Servlets -> Connection Pooling
    Will a started thread run to completion in this example?