Jay Kniffen
Jay Kniffen

Reputation: 67

Java Beginner - What is wrong with my method?

I'm a Java beginner and I'm trying to create a really simple method in NetBeans, and I'm frustrated that this doesn't work. Can someone point out my errors?

public static void main(String[] args) {
    
    /**
 *
 * @param name
 * @return
 */
    public String critMeth(String name){
        String c = name + " loves you!";
        return c;
    }
    String critter = "Henry";
    String love = critMeth(critter);
    System.out.println(love);
}

Upvotes: 0

Views: 378

Answers (3)

CodeIT
CodeIT

Reputation: 1

The main method is the method java will run if you press start. It is not allowed to place a method inside of another method

public static void main(String[] args) {
    String critter = "Henry";
    String love = critMeth(critter);
    System.out.println(love);
}

public String critMeth(String name){
    String c = name + " loves you!";
    return c;
}

Upvotes: 0

Denys Séguret
Denys Séguret

Reputation: 382170

In java you don't declare methods in methods. Change that to

public String critMeth(String name){
    String c = name + " loves you!";
    return c;
}

public static void main(String[] args) {
    String critter = "Henry";
    String love = critMeth(critter);
    System.out.println(love);
}

And don't forget you can only call static methods from static methods if you don't call them on receiver objects. So make the first method static (I let that as an exercise to you).

Upvotes: 8

Gab
Gab

Reputation: 8323

public static void main(String[] args) {
    String critter = "Henry";
    String love = critMeth(critter);
    System.out.println(love);
}

/**
 *
 * @param name
 * @return
 */
private static String critMeth(String name){
    String c = name + " loves you!";
    return c;
}

Upvotes: 1

Related Questions