FAQ: Learn Java: Methods - Adding Parameters

This community-built FAQ covers the “Adding Parameters” exercise from the lesson “Learn Java: Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Adding Parameters

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

what if we do not use the constructor method ? Can we still use for example: public void startEngine method?

hey program starts from main(), so we have to call methods in class from main(). to use class features you have to define a object. therefore, you have to create a object .

Hey @beta9836886690, @giga3691109859, welcome to the forums! @giga3691109859 is right… you have to define a main method otherwise Java will throw an error. (that is where the program starts from, after all.)

Hey, I’ve been doing this practise and ended up getting the following error:
./Store.java:8: error: illegal start of expression
public void greetCustomer(String customer){
^
And these giant blocks of errors:
public void greetCustomer(String customer){
^
Store.java:14: error: class, interface, or enum expected
public void advertise() {
^
Store.java:16: error: class, interface, or enum expected
System.out.println(message);
^
Store.java:17: error: class, interface, or enum expected
}
^
Store.java:20: error: class, interface, or enum expected
public static void main(String args) {
^
Store.java:23: error: class, interface, or enum expected
}
^
6 errors

What do I do?

Hello @tag0161023004 and welcome to the Codecademy Forums!

Please post your code and format it using the </> button so we can see exactly what the problems are.

Oh no, I have already checked the solution. I needed to put. public void advertise() out of the public Store method

1 Like

public class Store {

// instance fields

String productType;

// constructor method

public Store(String product) {

productType = product;

public void greetCustomer(String customer) {

  System.out.println("Welcome to the store, " + customer + "!");

}

}

// advertise method

public void advertise() {

String message = "Selling " + productType + "!";

System.out.println(message);

}

// main method

public static void main(String args) {

Store lemonadeStand = new Store("Lemonade");

}
I have everything where it said to put it and where it automatically was yet i still get errors and can’t print the message and the errors are the exact same as the ones that tag got
Edit: actually prob bc i didnt have the declaration of the method

You have several errors here

    public Store(String product) {

    productType = product;

What are you missing here?
Return type for the method is missing but its a constructor. So why do we get this message? Hint would be your missing something at the end to close this.

    public void greetCustomer(String customer) {

        System.out.println("Welcome to the store, " + customer + "!");
    }

    }

Same thing, do you notice something wrong at the very end of this? This might be the reason you having an error with your constructor. You cannot (correct me if I am wrong) define greetCustomer inside the constructor.

Store lemonadeStand = new Store("Lemonade");
}
public class Store {

    // instance fields

    String productType;

    // constructor method

    public Store(String product) {

        productType = product;

    }

    public void greetCustomer(String customer) {

        System.out.println("Welcome to the store, " + customer + "!");

    }

    // advertise method

    public void advertise() {

        String message = "Selling " + productType + "!";

        System.out.println(message);

    }

}