Color theme
0 day streak0 XPP1
JavaLearn mode
MISSION 1 OF 1

Label a parcel object

0 XP earned
Java track: IntermediateMission: IntermediateConstructorsInstance fieldsName shadowing
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.

x
0 / 2 challenges
LEARN THE CONCEPT

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.

Main.java
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.