Why are there so many errors


#1

this is my code:

class Dog  {
   
  int age;
     public Dog(int dogsAge) {
          
       public void bark(){
         System.out.println("Woof!");
       }
          age = dogsAge;
     
       
       
       public static void main(String[] args) {
    Dog spike = new Dog(2);
  }
 }
}

and these are the errors:
Dog.java:6: error: illegal start of expression
public void bark(){
^
Dog.java:6: error: illegal start of expression
public void bark(){
^
Dog.java:6: error: ';' expected
public void bark(){
^
Dog.java:13: error: illegal start of expression
public static void main(String[] args) {
^
Dog.java:13: error: illegal start of expression
public static void main(String[] args) {
^
Dog.java:13: error: ';' expected
public static void main(String[] args) {
^
Dog.java:13: error: '.class' expected
public static void main(String[] args) {
^
Dog.java:13: error: ';' expected
public static void main(String[] args) {
^
8 errors

what does this mean ?


#2

You've put your bark() method inside your Dog constructor. Close the constructor before you begin the bark() method.


#3

Hey @ftcteam! Just got a problem on your code! First remember you cant start a constructor over a method and your main.

Like this:

public Dog(int dogsAge) {
          
       public void bark(){
         System.out.println("Woof!");
       }
          age = dogsAge;
     
       
       
       public static void main(String[] args) {
    Dog spike = new Dog(2);
  }

Just seperate them like this:

public Dog(int dogsAge) {
    
  	age = dogsAge;
    
  }
  
  public void bark() {
		
    System.out.println("Woof!");
    
	}

Just close the constructor then do the methods! Hope it helped! If it didnt please reply saying so! And remember Java is the best!