Advertisement

Does Java main class run multiple times

Started by June 26, 2017 07:14 AM
5 comments, last by Lactose 7 years, 5 months ago

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. 

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.

blah :)
Advertisement

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.

 

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

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

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.

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement