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

2 Likes

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);

    }

}

Is it just me or does the for Adding Parameters lesson have an error in it? It gives the example:

class Car {
 
  String color;
 
  public Car(String carColor) {
    color = carColor;         
  }
 
  public void startRadio(double stationNum, String stationName) {
    System.out.println("Turning on the radio to " + stationNum + ", " + station + "!");
    System.out.println("Enjoy!");
  }
 
  public static void main(String[] args){
    Car myCar = new Car("red");
    myCar.startRadio(103.7, "Meditation Station");
  }
}

But surely this code wouldn’t work because the variable station from the start radio method is undefined?

2 Likes

You’re right, this is a bug, I’ve reported it, thanks!

1 Like

I came here just to see if anyone else noticed it too and if this was correct and not a typo. Glad to know it was just a typo and my bug finding skills are developing early :grinning:

2 Likes

Why do we put the variables in the constructor method in the instance field but when we don’t put the the variables made in the advertise method in the instance fields?

Hello
I think the purpose is to separate the tasks that are doable to a “Store” into methods because of a style called OOP. (Object-oriented Programming)

Correct me if I am wrong. Any feedback is greatly appreciated!

Haha yes i was about to comment the same thing right now. Glad someone already noticed and posted it!

can someone explain why we need to put “void” on greetCustomer and not on the store constructor method?

Will this course teach me how to take user input to create an object? It would be useful and more practical than me creating my own made up objects. I want to be able to learn how to connect user input with my classes that I’m creating.

we keep void, so that the method doesn’t return anything. You’ll learn more about these in future lessons. hope this helps.

I found the “Adding Parameters” exercises to be very confusing! There should be a spot in the code designated for the greetCustomer method, otherwise the user ends up trying to put that method elsewhere and then has problems. I got around the issue by putting that method into the “main” method area but it’s an imperfect solution. Codecademy should add an area that says //greetCustomer Method to avoid confusion