Reputation: 57
In tutorials on the internet I usually see pieces of code within public static void main(String[] args)
, whereas the lecturer for my programming course usually writes a method and calls that method within main.
For example, in tutorials I'd see:
class Person {
public static void main(String[] args) {
int age = 20;
System.out.println(age);
}
}
My lecturer would write:
class Person {
void run() {
int age = 20;
System.out.println(age);
}
public static void main(String[] args) {
new Person().run();
}
}
Is there a difference between these two, or is it just a matter of preference?
Upvotes: 3
Views: 2396
Reputation: 193
Both are ok for running simple samples or doing tests.
If you wish to just test some code/api quickly I would advise to run the first one - directly in the main method. So no need to create a new method if you just care about running your stuff and checking results printed.
However, if you are going to do something using more OO principles you should normally have the logic inside your objects in a non-static ( i. e not 'main') context. So if you have a logic which belongs to Person, that should be encapsulated inside Person's methods and whoever call it (another object or you simple main) will use its methods.
If you have questions regarding static vs non-static I suggest to read some tutorials over internet and make experiments. Example: http://beginnersbook.com/2013/05/static-vs-non-static-methods/
Upvotes: 1
Reputation: 816
From output perspective there is no difference as both will display the same thing. and from the technical perspective first example is even faster than the second example. It's a standard coding practice which makes the difference here. As being a beginner, the first example might look better solution as it is easier, faster and straight forward compared to the second example (which has extra effort for writing new method, creating new object then calling the method to achieve same thing which you could achieve with single line of code in the first example.)
The technical difference is already mentioned by @adam-arold. I will emphasize on programming principle which is the major difference here.
The second Example follows the Standard coding principle called SoC (Separation of Concerns). SoC says each Class/Method
should be having codes only related to what they are supposed to do, for any extra functionality should be handled in separate Class/Method
. Main
method is an entry point and responsibility for Main
method is to configure the setup and initiate your program rather than handling any business logic (here business logic is to print a line).
Your lecturer is following the standard practice :) even with simple examples.
Sounds like your lecturer is pretty good programmer :)
Upvotes: 1
Reputation: 1898
The main method in your class is your entry point to the program. That means that it should only be used to instantiate any object needed to make what the program will do.
Your program should be made out of classes, and some instances of those classes are the ones who will actually be making all the work, those "objects" are the ones that should have some behaviour that gives your program functionality.
The example you show is very basic. So basic it basically does nothing, but perhaps your lecturer wants to start getting you used to Object Oriented Programming without you even knowing it.
Later he might teach you that the public static void main
function will only make a call to a Form
object (a window) and that form will have a run method too. Everything that happens later will be responsibility of that Form and its components (i.e. handling user input like clicks or keyboard). The main function will do almost nothing afterwards.
Upvotes: 1
Reputation: 30548
The difference is that the main
method is static
which basically means that it has no access to non-static methods/fields of Person
. When you create a new instance of Person
with new Person()
you get access to all its member methods (like run
in this example).
Upvotes: 4