}
}
Let’s see…. there’s a class called
HelloWorld
. There’s a method called
main()
. And…well….
Uhh…
Um.
:thinking: emoji
Hm… what
does “Hello, World!” have to do with object-oriented
programming?
Spoiler: it really doesn’t… no objects here.
It’s demonstrating one of the few concepts in Java that are
not object-
oriented:
static methods. Ones that exist outside the realm of objects. God
dammit Java. Why can’t your “Hello, world!” program be less stupid?
But I should calm down. It’s bad for my health. Let’s talk about what’s
really happening in the main method.
Main method
“Who do you think you are? What gives…what gives you the right?” –
Michael Scott
The
main method is
the entry point to the program; when the program is
run, the main method is run.
The line of code that has the words “Hello, World!” is inside the main
method.
Anything you put between those curly braces {} is inside the main
method, actually.
public class
HelloWorld {
public static void
main (String[] args) {
System.
out
.println(
"Hello, world!"
);
}
}
Why is there a “main” method? What makes it so special?
The main
method exists because
something needs to run when the program starts.
Remember the whole “which came first, the chicken or the egg” dilemma?
Some say the chicken came first, because all eggs come from
chickens.
Some
say the egg came first, because all chickens come from eggs.
The answer in Java is that
neither of those objects came first. The main
method did.
Nothing can exist without the main method running first, outside of the
realm of objects.
This is what that static bullshit means.
Static properties
and methods exist
without objects.
“No object, no problem.” – the static keyword
Alternatively…
“Don’t make things static unless you have to because objects are good.” –
me
If you’re not 100% on this yet, don’t worry about it too much. Only a little
bit.
Okay , you might be thinking. But what about that other stuff in front of the
word
main ?
public ? Or
void ? This is just confusing.
I will cover this more thoroughly later in the book. But I’ll
briefly answer
this now as well because I hate when you want to know something, and the
author is just like “hold on we’ll get there.” Bitch I’ll tell you now.
Putting
public in front of something says that all the other classes,
even in different folders, can see it. You
could also describe some of
your code as
private if it is going to the bathroom and needs privacy
or something, but not the main method! It’s always out in public!
Putting
void in front of some code means that it does some cool shit
but doesn’t report back. No one asks
the main method to say if
things went well or not, or for the value of some math calculation.
Main method doesn’t care if anyone knows.
Got it? If not, read those explanations one more time.
…Okay, fine you’re not going to. I’ll summarize even more briefly:
The main method is how Java begins running your program.
The main method is
static because
something needs to exist outside of the
realm of objects. And the developers of Java said so.
The main method is
void because it doesn’t
need to provide a value to
whoever is running it. And the developers of Java said so.
The main method is
public because it will be called by someone outside of
this folder, namely Java. And the developers of Java said so.
Declaring a class
Let’s leave “Hello, World!” in the dust for now where it belongs, and I’ll
explain
in detail how you declare a class:
Do'stlaringiz bilan baham: