Hey, I think sometimes we can benefit while coding high end concepts by taking a step back and examining the building blocks and frames. That is why today’s topic may seem a little short and boring, but I’ve definitely found that a lot of the time I get too hung up on what my end result should be that I don’t take enough time to worry about how I’m starting my code, the minutia that makes up just about every program. D-Did you like that big word there, minutia? Yep, that’s what I went to college for! 😀
Back to business: there’s a sort of general template that a majority of Java programs follow.
A. Opening Comment – Comment to describe the code’s purpose
/*
This code shows the basic setup of a Java Program
It asks the user for input and checks it
in a if loop to return a message to the user
*/
B. Import Statement – Import any Java packages, if needed.
import java.util.Scanner; //importing the Scanner class
C. Class Declaration – the class is a user-defined blueprint to create objects from
//Name must be the same as the name of your file!
class DemoClassMethod {
}
D. Class Method Declaration – methods are blocks of code which are used to perform tasks
class DemoClassMethod {
//This is kind of empty huh? Let's declare a method!
public static void main(String[]args) {
/*What's all that stuff? Let's talk about it!
1. public and static are both access modifiers
in this method's case;
-public means that any class can access this method
despite what class it was created in
-static means that this method doesn't
belong to an instance of the class,
but to the class itself
2. void represents the return type; in this case
nothing is returned
3. main() is the method's name
4. String[]args is the parameter handed to the method.
*/
}
}