1

I'm doing a beginner's exercise, and the goal is to create the below output:

Hello Billy, it's good to meet you!

Hello Jake, it's good to meet you!

Hello Michael, it's good to meet you!

Below is my code:

package apollo.exercises.ch03_methods;

public class Ex2_SayMyName {
    public static void main(String[] args) {
        String name1 = "Billy";
        String name2 = "Jake";
        String name3 = "Michael";
    }

    public void SayMyName(String name){
        System.out.println("Hello " + name + ", it's good to meet you!");
    }

    SayMyName(name1);
    SayMyName(name2);
    SayMyName(name3);
}

I get errors at SayMyName(name1); and the } above that line.

For }, it says I need to insert another } to complete the class body, but isn't it already complete?

For SayMyName(name1), I get "Syntax error, insert "... VariableDeclaratorId" to complete FormalParameterList"

What am I doing wrong? :(

Community
  • 1
  • 1
Kenneth Yong
  • 375
  • 1
  • 6
  • 14

5 Answers5

2

Put these lines in main() method :

SayMyName(name1);
SayMyName(name2);
SayMyName(name3);

And make public void SayMyName() static like this :

public static void SayMyName(String name){
    System.out.println("Hello " + name + ", it's good to meet you!");
}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
2

Move the method calls inside your main method

public static void main(String[] args) {
    String name1 = "Billy";
    String name2 = "Jake";
    String name3 = "Michael";

    SayMyName(name1);
    SayMyName(name2);
    SayMyName(name3);
}

Also, if you don't want an instance of the class Ex2_SayMyName when calling SayMyName you must make the method static, chage to this:

public static void SayMyName(String name)

And one last nitpick, respect the java naming conventions and rename your method to start with lowercase. Variables and methods always start with a lowercase letter and classes name start with an uppercase one.

MihaiC
  • 1,618
  • 1
  • 10
  • 14
1

You are calling SayMyName function in class boundary(outside any methods and blocks) which is not possible. In class boundary only declaration and instantiation can be done.
Put SayMyName inside main or any other method.

public static void main(String... args)
{
    //your code here
    SayMyName(name1);
    SayMyName(name2);
    SayMyName(name3);
 }

And change the SayMyName method to static.
EDIT- Class Boundary <- outside any methods and blocks

Devavrata
  • 1,785
  • 17
  • 30
  • that's not quite true, i can put static blocks that print values on screen in that space during class loading – MihaiC Dec 19 '14 at 14:13
  • Yes but that need static variables means name1,2,3 should be static variable.But here op is declaring the variables in main method.Not only static but you can put your code in anonymous block also. – Devavrata Dec 19 '14 at 14:15
  • my point is "only declaration and instantiation can be done" is wrong. I can do this: static {Sytem.out.println("abc");} in the class boundary, it compiles, works and even writes abc to console during class load – MihaiC Dec 19 '14 at 14:16
  • Yes, try out my example – MihaiC Dec 19 '14 at 14:19
  • @MihaiC I know there is concept of static & initializer block. But my point is that in class boundary(outside blocks & method) calling statements are not valid. – Devavrata Dec 19 '14 at 14:30
0

What is executed is the main method of your class, so, putting the calls to SayMyName outside it does not make the desired effect. You need to put the call to your method inside the main method. Also, the SayMyName has to be static in order for it to be called from the main. You might want to see this (about static methods in java) just like this:

package apollo.exercises.ch03_methods;
public class Ex2_SayMyName {
public static void main(String[] args) {

    String name1 = "Billy";
    String name2 = "Jake";
    String name3 = "Michael";

    SayMyName(name1);
    SayMyName(name2);
    SayMyName(name3);
}

public static void SayMyName(String name){
    System.out.println("Hello " + name + ", it's good to meet you!");
}
aviad
  • 8,229
  • 9
  • 50
  • 98
  • it will not compile saymyname is not static – MihaiC Dec 19 '14 at 14:08
  • @aviad Thanks for the link to static classes! I just want to make sure that I understand. Making a method static is to prevent it from being affected by other elements in the same class right? – Kenneth Yong Dec 19 '14 at 14:40
  • 2
    @KennethYong - to declare that this method does not change any class members – aviad Dec 19 '14 at 14:44
  • @KennethYong you might also want to read this: http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods It gives example of when you should use static classes – MihaiC Dec 19 '14 at 14:56
0

Call the methods from main by moving the following in main:

SayMyName(name1);
SayMyName(name2);
SayMyName(name3);

So that your program looks like this:

public class Ex2_SayMyName {
public static void main(String[] args) {

    String name1 = "Billy";
    String name2 = "Jake";
    String name3 = "Michael";

    SayMyName(name1);
    SayMyName(name2);
    SayMyName(name3);
}

//note the static in the below line
public static void SayMyName(String name){ 
    System.out.println("Hello " + name + ", it's good to meet you!");
}
}

If you want to call your methods directly without making an instance of the class,then you need the static keyword in the declaration of the method. This is what I have done in the above program.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83