Jump to content
  • Advertisement
Sign in to follow this  
Xer0botXer0

Does Java main class run multiple times

This topic is 535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

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

Yes, that is how most servers operate. We can't comment on "this server example" because you've not explained what that is.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!