Expression expected java

Viewed 34

Hello I am a beginner in java.

   class Animal{
   String name;
  void Sound(){
   System.out.println("I am  an animal");
  }
 }

 public class Cat{
  public void main(String[] args){
  void Triangle(){     //I think here is the problem
   System.out.println("Triangle");

    }
  } 
 }

I am getting this error Expression expected.

2 Answers

You look like you're trying to declare a method inside of another method. Put the declaration outside of the method. (Pay attention to the open and closed braces. You need the final closing brace "}" of the main method, then you put your declaration after the brace.)

 public class Cat{

  public void main(String[] args){
  } 

  void Triangle(){     //I think here is the problem
   System.out.println("Triangle");
  }

 }

You are declaring a method inside of another method. Java does not allow this. Move your void method "triangle" outside of the main method and declare it as static, then "invoke" (or call it) like this:

public static void main(String[] args) {
     triangle() // invoke
}

static void triangle() {
// Your code
}

Your main method also appears to not be declared as "static". The JVM (Java virtual Machine) will not recognize it as your programs main method and won't start. If you are invoking methods from a static method, the method you are calling, must also be static.

Related