Java programming: the Pizza class, continued
The tutor continues with some points about Java classes.
In my previous post I introduced a Java class called Pizza. Here are a few points that might come to mind to someone new to the topic:
- The class name is capitalized by convention. In Java, classes have capitalized names, while methods have lower case names (with one exception which we’ll visit soon:). Furthermore, a class file must be saved as classname.java for the compiler to be happy. This file must be saved as Pizza.java for that reason.
- This Pizza class won’t “run”. From a C or Perl point of view, it’s not a program; rather, it’s more like a data structure. A simple perspective suggests it won’t run because it lacks a main method. Therefore, to be put into action, it depends on another class that has a main method.
- The capitalized method Pizza(String[] toppingslist) inside the class Pizza is a constructor. A constructor is a method used to create an instance of a class, simultaneously giving values to certain of its fields (aka variables). The Pizza class constructor accepts an array called toppingslist, which serves to identify its toppings.
- The fact that the method describeMe() is public means it can be called freely by other classes that want to use it.
The rest of the code in the Pizza class involves specific instructions from the Java language. I’ll illuminate a few points towards it in future posts:)
Source:
Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.
Leave a Reply
You must be logged in to post a comment.