In Python, JavaScript (no relation), and other languages, you can just say print(Hello) and your program will work assuming you have configured everything correctly—that is, without all the “public class” or “public static void main (String[] args)” boilerplate that Java requires even for the bare minimum.
Imagine Jeff, a real baker. Jeff belongs to the class “Baker” and Jeff can do things every baker can: he can make cookies, he can bake cakes, he can apply frosting and icing, he can decorate cakes, and so on. In the society where Jeff lives, no one doesn’t have a job: Jeff’s job is “Baker”. And everyone who has a job can do all the tasks that the job is responsible for: in Jeff’s case, baking cakes, making cookies, etc..
By virtue of being a Baker, Jeff also has a few characteristics by which we can describe him: his name, his age, where the baker lives, and so on.
That’s a pretty good analog for Java classes.
In Java, everything belongs to a class. Classes describe objects: Bakers, Students, Classrooms, and more. Those classes have fields (more on them later) and methods—we’ve already seen at least one, many times, main! In our linear search example we saw linearSearch.
Methods have six defining attributes:
- The class they belong to (which may never be omitted)
- Their access modifier (which can be omitted, but that means something)
- Whether they are static or not (which can be omitted, but that means something)
- Their return type (which may never be omitted)
- Their name (which may never be omitted)
- Their arguments (which might not be any, but must always be present even if so)
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
There is one method inside the class HelloWorld: main
- It’s associated with the class HelloWorld
- It is public
- It is static
- It has a void return type
- Its name is main
- It takes one argument, an array of Strings called args
For now, you need to learn this list of six items, but not yet what most of them are or why they’re important. That will come later:
- Associated class
- Access modifier
- Static or not
- Return type
- Name
- Argument list
Those six things always show up in this order.
Look now at HelloWorld modified to include both main and sayHi()
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
public static void sayHi() {
System.out.println("Hi");
}
}
sayHi():
- Is in HelloWorld
- Is public
- Is static
- Returns void
- Is named sayHi
- Takes in nothing
One method reigns supreme: main. The Java compiler—the program that allows your program to be converted from human-friendly to computer-friendly—looks for a method somewhere in every class that is:
- In the class
- Public
- Static
- Returning void
- Named main
- Taking one argument, an array of strings with a name
If a method does not exist with those attributes exactly—for example, if you have private int main(int x), but not public static void main(String[] args)—then your class will not be executable.
Execution always starts from main, and then follows the instructions in main, jumping into and out of other methods as directed there.
This list of six attributes—class, access, static modifier, return type, name, arguments—constitutes the method’s signature. Much like a human signature, it identifies the method and reveals important information about it.
You can have very similar signatures in the same class. You could have both of these methods:
public static void main(String[] args)
and
private static void main(int x)
The same class, however, may not contain two methods where all six attributes (class, access, static flag, return type, name, parameter list) are identical.
The benefit of writing classes and methods is that it makes it much easier to reveal only what is absolutely necessary at the time and prevents you from having to rewrite duplicated code over and over.
Classes describe objects (Baker classes describe bakers, Chef classes describe chefs, Teacher classes describe teachers, and so on), and the methods inside them give the instructions for things those objects can do.
Before we go, there’s one very important kind of method that breaks some of these rules: a constructor. You’ve seen constructors already, as in Random randy = new Random();
There are two basic types of constructors, and you’ve actually seen them both already:
- The default/no-args constructor, seen above
- The parameterized constructor, which is something like Random randy = new Random(100); (in this case, 100 is a “seed value” that basically tells Random to generate a new random sequence)
But constructors have a few special rules
- They have an access modifier just like other methods, which most often is public but it need not be
- They are not marked as static (for a reason we’ll explore soon)
- They never have a return type
- They can have parameters or not
- Their name—Dog() when the class they’re for (and in) is Dog, Cat() when it’s Cat, Student() when it’s Student—matches their class
- If none is defined for a class, the no-args one is given by default
- If any are defined, you lose the no-args unless you define it yourself
Constructors exist to create objects of their type in memory. Where that gets done is under the hood to a degree that you don’t need to know or care. Dog() creates a Dog object, Person() creates a Person object, and so on.
You call these constructors in this way:
Class objectName = new Class();
or
Class objectName = new Class(paramter1, parameter2, parameter3….);
You can define as many of these as you want in your classes.
Happy constructing!
No comments:
Post a Comment