Label a parcel object
Before you start this intermediate track
Variables, methods, and typed collections. The lessons use constructors, fields, and maps.
Guided code reading and debugging. Runnable coding challenges are available in JavaScript, Python, and SQL.
Understand it, then try it.
A class describes the data each object can hold. Its constructor runs when new creates an object. The parameter and field here share the name label, so this.label identifies the current object’s field. Storing the parameter there makes the chosen label available after construction, when the object is used elsewhere.
class Parcel { String label; Parcel(String label) { this.label = label; }}public class Main { public static void main(String[] args) { Parcel box = new Parcel("FRAGILE"); System.out.println(box.label); }}Each example is a complete Java 11+ source file named Main.java. Compile with javac Main.java and run with java Main. Each mission is independent.
Track the values line by line. Predict the result before running this example in your own editor.