[java] Java Design Practices
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.
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: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.
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() { }}
I might have just confused you more, but hopefully it made sense.
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.
Yup all you really need is this some where:
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)).
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)).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement