Get startedGet started for free

Declare now, assign later

In Java, variables can be declared first and assigned a value later, giving your code flexibility. In this exercise, you'll declare a String variable and then assign it a list of ingredients. Let's get cooking! 🍫🥚🥛

This exercise is part of the course

Introduction to Java

View Course

Exercise instructions

  • Declare a String variable called ingredients without assigning it any value.
  • Assign ingredients the value of "chocolate, eggs, milk, flour, sugar, vanilla".

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

class DeclareThenAssign {
    public static void main(String[] args) {
    
    	// Declare String variable called ingredients
        ____ ____;
        
        // Assign ingredients value
        ____ = "chocolate, eggs, milk, flour, sugar, vanilla";
        
        System.out.println(ingredients);
    }
}
Edit and Run Code