Jump to content
  • Advertisement
Sign in to follow this  
eben

[java] Java Design Practices

This topic is 4825 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

When a java application enters through public static void main(String[] args) it requires that every method called from main be static as well. How can I design a class so that after entry through the main method I can can call other methods without them having to be being static. I basically dont want to end up with a class full of all static methods. What is the design convention for an issue like this? thanks.

Share this post


Link to post
Share on other sites
Advertisement
The static keyword means that that one function/object is one function/object no matter how many instances there are. Static functions/objects don't need to be initialized.

For example:

public class StaticTest {

static String string;
int number;

public static void main(String[] args) {
StaticTest st = new StaticTest();//create an object
StaticTest st2 = new StaticTest();//create another object
st.string = "blah";//This won't work because string is static, instead call it like the following line
StaticTest.st = "blah";//This will work
st2.number = 5;//Will work
StaticTest.number = 5;//Won't work
st2.test();//This won't work because it is static
StaticTest.test();//This will work
st.test2()//Will work
StaticTest.test2();//Won't work

}

static void test() {
}

void test2() {
}

}
Anyway, if you create an object first, the functions/members don't have to be static.

I might have just confused you more, but hopefully it made sense.

Share this post


Link to post
Share on other sites
H_o_p_s gave a really good (if not rather elaborate) explanation of how to do this. I rather prefer having a single class called Main that has the main() method in it. The main() method will create an instance of an Application class and call some method on it (maybe start() or something). I've always found the idiom of putting everything in a single class confusing and quite anti-Java in design.

Share this post


Link to post
Share on other sites
Yup all you really need is this some where:


public class Main {
public static void main(String[] args) {
new App().start();
}
}

// A bit more laborous, but fun (to me) approach:

public class Main {
public static void main(String[] args) {
new EntryController().setArgs(args).start();
// setArgs() takes a String array, and in the end returns 'this'
// this particular start also return 'this'... because I want it to
}
}





Now write, App(), or EntryController, or Controller, or whatever. This works because 'new' has a higher precedence than the '.' operator.

L-

PS: You, instead, could write a static block. Get a class loader, then instantiates some class, that is Runnable, and then call:

new Thread(app).start()

(As long as you are not assigning the variable, because that start returns void (horible nomenclature)).

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!