Sign in to follow this  
Xer0botXer0

Does Java main class run multiple times

Recommended Posts

Hi guys,

Can I see Javas Main class as a main loop in which everything inside will be run continuously ?

This is confusing because at the start of the main class in my code sample two class objects are created and one array object. I'm not sure about the process.

In GML there's a section where code is only run once, and then there are sections where scripts are continuously run. And of course if I only want code thats in the section which runs continuously to only run once I'd use a boolean. 

Share this post


Link to post
Share on other sites

The main method is only called once. When execution reaches the end of the method, it exits and (if no other threads are running) the application terminates. If you want to continuously run code you'll need a loop of some kind.

Share this post


Link to post
Share on other sites

Thank you,

This is kind of a dilemma, how would I keep my program from not closing on its own ? 

It doesn't make sense that a program should ever close on its own and only when specified by a close button or such.

I'm trying to create a server, so what if I do something like..

public class main{

boolean awake = true;

while (awake)

{//run all the code}

}

Would this work ? In this server example I dont see anything like this. So I'm not sure how it's staying alive.

 

Edited by Xer0botXer0

Share this post


Link to post
Share on other sites

That would indeed work, and then to close it all you would need to do is have something set awake to false.

As Kol said, the Main will run through once so unless it has something like a loop in it to keep it running the code it will just hit the end and close. A server generally just has a loop wrapping some kind of waiting code and then executes functions based on what clients send to it, obviously not seeing what code youre copying / learning from so cant see what its doing but there should be a loop or something int here keeping it alive

Share this post


Link to post
Share on other sites

This is also how most games operate. Most applications of any description, really, so it's not isolated to just server type applications.

Notable exceptions are things like tools that operate on some input and spit out some output. These things generally just start, do their assigned task and shut down, and do not have such a loop.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this