Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By jim829

Thread: Cannot resolve to a type

  1. #1
    Join Date
    Oct 2016
    Posts
    47
    Rep Power
    0

    Default Cannot resolve to a type

    My main method is in a class called "Home" which is in the default package. In the main method, I am attempting to create an object of the "Breed" class (in the animal package), then invoke a method called getDogBreed(). The Breed class, is in the same Java file as the Dog class. I am getting an error on line 23 about not being able to resolve to a type. After my original post, I realized I forgot to include all animal classes in the import statement whereas before I only had dog. After including the import statement, I'm still getting an error.

    I have 2 pics attached of the classes in question. The 2nd pic is not embedded. It is hyperlinked instead. Not sure why.

    What is causing this error?
    Attached Thumbnails Attached Thumbnails Cannot resolve to a type-getdogbreed-method.jpg   Cannot resolve to a type-breed-cannot-resolve-type.jpg  
    Last edited by allDogsAreGood; 09-28-2018 at 03:38 AM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,971
    Rep Power
    7

    Default Re: Cannot resolve to a type

    Give Breed its own file. Name it Breed.java and make the class public. Don't take the easy way out and put everything in one file. That doesn't work in Java.

    Rule of thumb: public classes need their own files.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: Cannot resolve to a type

    In the future, would you please not post images. They are difficult to read. The normal procedure is to simply copy and past your code in the body of the message. Then surround your code with code tags. See the BBCODES section. An easier way to do this is to go in the advanced editor (bottom right button of your window) and highlight your code. Then click the # button. It will put the code tags there for you. You can also preview your post before you send it.

    Regards,
    Jim
    SurfMan likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Join Date
    Oct 2016
    Posts
    47
    Rep Power
    0

    Default Re: Cannot resolve to a type

    Quote Originally Posted by SurfMan View Post
    Give Breed its own file. Name it Breed.java and make the class public. Don't take the easy way out and put everything in one file. That doesn't work in Java.

    Rule of thumb: public classes need their own files.
    Ok thank you. So each tab/java file can have only one public class.

    If we use the Dog class file as an example, we have Dog as the public class. In practice, what are some other classes that would appear in the Dog class file?

    I would think that each tab would have some non-public classes instead of only having one public class. I'm trying to get an idea of what some of these other classes might be.

  5. #5
    Join Date
    Oct 2016
    Posts
    47
    Rep Power
    0

    Default Re: Cannot resolve to a type

    Quote Originally Posted by jim829 View Post
    In the future, would you please not post images. They are difficult to read. The normal procedure is to simply copy and past your code in the body of the message. Then surround your code with code tags. See the BBCODES section. An easier way to do this is to go in the advanced editor (bottom right button of your window) and highlight your code. Then click the # button. It will put the code tags there for you. You can also preview your post before you send it.

    Regards,
    Jim
    Ok I will keep that in mind. The reason why I included the picture was so that anybody can also see the other tabs/java files that are involved. I thought that would be more helpful to give the big picture. I will remember your suggestion.

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,971
    Rep Power
    7

    Default Re: Cannot resolve to a type

    Quote Originally Posted by allDogsAreGood View Post
    Ok thank you. So each tab/java file can have only one public class.

    If we use the Dog class file as an example, we have Dog as the public class. In practice, what are some other classes that would appear in the Dog class file?

    I would think that each tab would have some non-public classes instead of only having one public class. I'm trying to get an idea of what some of these other classes might be.
    Having multiple classes in a single file does not happen very often. At this point, assuming you're learning Java, you should stick to putting one class in one file. How small the class is, doesn't matter.

    The class that is defined inside another file, or even inside another class, is tightly coupled to the file or class that it is created in. This has its specific use cases. For example, java.util.ArrayList uses a custom implementation of java.util.Iterator which is defined in the class ArrayList itself. Open the source file of ArrayList to see what I mean, search for "private class Itr". This class Itr can only be used from inside the ArrayList class. That's VERY tightly coupled, but in this case useful.

    At this point I wouldn't worry too much about it. You should get used to having lots of multiple files open. It's the quality of the editor that decides if you're going crazy or not :) (Can I shamelessly plug JetBrains's IntelliJ again?)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: Cannot resolve to a type

    But only if you use the private modifier as you did with Itr. Multiple classes defined in a single file (even inner classes within a non public class) are visible to all other classes that are in the same package.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,971
    Rep Power
    7

    Default Re: Cannot resolve to a type

    One more common scenario for exposing an inner class (A class defined in another class) is the builder pattern:
    Java Code:
    public class ComplexObject {
    
        public static class Builder {
              //methods to build a ComplexObject
              public Builder withName(String name) {...}
              public ComplexObject build() {...}
        }
    
    }
    This is instantiated by calling:
    Java Code:
    ComplexObject co = new ComplexObject.Builder().withName("Foobar")....build();
    But don't let this distract you from learning Java :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 2
    Last Post: 12-21-2012, 04:32 AM
  2. Replies: 6
    Last Post: 09-14-2012, 12:33 PM
  3. Replies: 4
    Last Post: 08-01-2011, 10:29 AM
  4. help me to resolve this error
    By akiravelmont in forum Web Frameworks
    Replies: 3
    Last Post: 05-17-2011, 02:04 PM
  5. Yet another Cannot Resolve Symbol
    By Gabinka in forum New To Java
    Replies: 5
    Last Post: 03-27-2009, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •