Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Methods in Java?

I am trying to understand methods in Java (Net Beans), however, I am currently stuck. I'm looking at an example from https://www.w3schools.com/java/java_methods.asp and I can't get it to build or run successfully. I'm really not sure what I'm doing wrong here. Conceptually, I understand what it is doing by taking the code and adding methods then outputing them. Would you be able to inform me of the full code from top to bottom of how to get the below program to run? Thank you!

public class MyClass {

static void myMethod(String fname) {

System.out.println(fname + " Refsnes");

}

public static void main(String[] args) {

myMethod("Liam");

myMethod("Jenny");

myMethod("Anja");

}

}

// Liam Refsnes

// Jenny Refsnes

// Anja Refsnes

2 Answers

Relevance
  • 2 years ago

    The code looks like it should work, and the first answer showed that it works in Eclipse.

    You didn't say what error messages you got when trying to build it. (NetBeans builds Java applications as you type in or edit the source code, flagging lines with errors on the left over the line numbers (a light bulb with a red circle around a ! symbol.) Hover the mouse pointer over that error icon to get a message. If you want a full list you can copy/paste or print out, then use the "Clean & Build" function (a hammer + a whisk broom icon on the standard tool bar)

    There are only two things I can think of that could make this fail under NetBeans: create the wrong project type, or typing/pasting that code for MyClass into a file that wasn't named "MyClass.java". I'll bet a nickel it's the second one. If you're seeing an error icon on the "public class MyClass {" line, then I'll up that bet to a dime.

    The easiest way to fix this is to build a new project, then copy/paste the code from the old one. Here's a set of steps that should work:

    1. Open the New Project dialog (File>New Project, Ctrl+Shift+N, or click the New Project toolbar icon.)

    2. Choose Java as the category and "Java Application" as the project type. Click Next.

    3. Type a project name ("MyClass" is good), but DON'T CLICK FINISH YET.

    From here, there are a few different ways to go. Easiest to just make the main class name "MyClass" without the "myclass." package name prefix. Or you can uncheck the box and make an empty project that you'll add a MyClass.java file to later. Or, you could leave it as is; and be sure not to delete the package statement at the top when you type/paste code. I'll pick the first for now. You can experiment with the other ideas later.

    4. Edit the main class name to read just "MyClass" and click "Finish".

    5 Finally, paste your code into the newly created MyClass.java file and press F6 to build and run.

    These steps are for NB version 8.2. If you have a newer version from Apache, the steps should be very close. Good luck.

    And remember, when you're asking about a build error, be sure to include the exact error message(s) you see. Clean&Build is good for getting that list.

  • ?
    Lv 6
    2 years ago

    I didn't see any problems, so I ran it in Eclipse. ran fine there. The problem must be with how you're compiling it. What is the error?

    I would recommend watching a "hello world" Java Youtube video to get you started.

Still have questions? Get your answers by asking now.