How to fix the symbol not found error in my code

10.3k views Asked by At
// An illustration of object creation.
class ShipMain1 {
    public static void main(String[] args){
    // Define a method variable to refer to a Ship object.
    Ship argo;
    // Construct a new Ship object.
    argo = new Ship();
    }
}

When I go to compile it, it tells me symbol can not be found for the Ship in both Ship argo and argo = new Ship(); please help I'm an extremely new beginner. I'm also copying this out of a programming book so I don't know why it's not working.

2

There are 2 answers

1
C.Champagne On

An object is an instance of a class. You need thus to have a class defined somewhere. In your case, you certainly forgot to copy the Ship class.

You can just create the missing class the same way you created ShipMain1. No need to create any method. The following should work.

public class Ship{}

A faster solution is to use the class where the main method is defined. You can rename the class ShipMain1 to Ship or set a argo as an instance of ShipMain1.

0
rishabh malik On

An object is an instant of a class, if you want to perform instantiation you have to declare the class somewhere in that folder as a new class which defines Ship with a constructor ship() which defines ship. And After that you can call it.

public class Ship
{
  public Ship(int a, int b)
  {
   sizeOfShip = a;
   sizeOfSails = b;
  }
}

After declaring this class you can declare a new instant of this class in further program or in any other class.