Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Object does not recognize variables but it's class does? (Feel like a noob :( )

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Object does not recognize variables but it's class does? (Feel like a noob :( )

    My first legit program :3 a pretty big and complicated one! Sigh... However I'm already having problems. Let me give you some background. For now I am simply logging a bunch of ships and their stats. I started out making each ship (like AdvHvgCruiser, below) a class of it's own. It worked great, but then I could not for the life of me remember why I did that instead of making them objects. I saved that project but reformatted it into this... And now I'm having problems


    Here's what I have in this class:

    import stocharactertracker.ships.Ships; (all of the variables are declared and set default in Ships)

    public class FedCruiser extends Ships {
    public FedCruiser() {
    this.shipType = "Cruiser";
    this.shipFaction = "Federation";
    this.cruiserSpecial = true; }
    (This is all fine, following is where the problem is)

    FedCruiser AdvHvyCruiser = new FedCruiser() {
    this.shipName = "Advanced Heavy Cruiser";
    ..... (A ton more declarations..) } }

    In the object I get "illegal start of type... Identifier expected"

    I don't know why .-.
    Please, explain and give a solution.should I switch bal to the classes idea?

    Thank you for your time ^.^


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Object does not recognize variables but it's class does? (Feel like a noob :( )

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    get "illegal start of type... Identifier expected"
    Please copy the full text of the error message and paste it here. It shows exactly where the error is located.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Object does not recognize variables but it's class does? (Feel like a noob :( )

    P.S. My wifi is down so I have been on my phone...
    import stocharactertracker.ships.Ships; //all of the variables are declared and set default in Ships
     
    public class FedCruiser extends Ships {
      public FedCruiser() {
         this.shipType = "Cruiser";
         this.shipFaction = "Federation";
         this.cruiserSpecial = true; }
    //This is all fine, following is where the problem is
     
       FedCruiser AdvHvyCruiser = new FedCruiser() {
            this.shipName = "Advanced Heavy Cruiser"; //ERROR: illegal start of type cannot find symbol: class shipName <identifier> expected
            // more declarations ERROR here as well

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Object does not recognize variables but it's class does? (Feel like a noob :( )

    The code tags you used don't work. Please fix them.

    Copy and paste the code and the error message when the internet connection comes back.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Object does not recognize variables but it's class does? (Feel like a noob :( )

    I see it in the code box that looks like an IDE , now if that's what you mean.

    You might have checked before I edited, I accidentally spelle highlight wrong in the first one

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Object does not recognize variables but it's class does? (Feel like a noob :( )

    cannot find symbol: class shipName
    Where is shipName defined? The compiler can not find its definition.

    more declarations ERROR here as well
    Hard to say without seeing what the errors are.

    Copy the full text of the compiler's error messages and paste them here.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Object does not recognize variables but it's class does? (Feel like a noob :( )

    I did put the full message of the error...

    "Illegal start of type
    Cannot find symbol: class shipName
    <identifier> expected"

    The IDE points this out to me before I even run

    shipName was a String declared in the Ships class, as previously stated...

    Wherein lies the question.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Object does not recognize variables but it's class does? (Feel like a noob :( )

    What are you trying to do? That anonymous class attempt looks wrong.
    Why not use the class's constructor?

    The compiler's full error message has a better explanation of the error. Trying to type in an IDE's messages makes for mistakes and confusion.
    A copy and paste of the compiler's error messages would be better.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Object does not recognize variables but it's class does? (Feel like a noob :( )

    There is no error in the compiler because I have not yet given it any output commands.
    "Anonymous class attempt"? Sorry
    I am only trying to set the variables I declared in Ships more specifically inside new objects in the FedCruisers class, if that makes sense

    --- Update ---

    The Ships class is only a list of

    <primitive> <primitive name> = <value>

    A rrrrreally long list, it's pretty pointless to put here. As I mentioned before, I was doing the same thing but with each ship (what I have tried making an object of the FedCruiser class here) as it's own class, but I thought this might make more sense for my application?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Object does not recognize variables but it's class does? (Feel like a noob :( )

    Try making a constructor to do the work.

    Calling a class's constructor from inside of its constructor can make an infinite call/loop possible.

    There is no error in the compiler
    Sorry, that is exactly where the error is detected. Your IDE hides it all.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Object does not recognize variables but it's class does? (Feel like a noob :( )

    Hmm sorry for all the confusion. Is there a way to make it show you the errors when you haven't asked for output? Is it better to just make them subclasses of FedCruiser like I did originally and then just create an object there, or make them all objects in the FedCruiser class? (i am relatively new to programming, much under 1 yr I'm sure you can tell x3)

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Object does not recognize variables but it's class does? (Feel like a noob :( )

    Either extend the class to create a new type
    or use the class's constructor to make each instance of the class different by passing it values as needed.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    Tea.EarlGrey.Hot. (March 19th, 2014)

  14. #13
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Object does not recognize variables but it's class does? (Feel like a noob :( )

    That will be an extremely long list of stuff in the constructor... So I think I will stick with the other way.
    Thank you very much!

Similar Threads

  1. Searching for object variables inside a 1-D array?
    By Scorks in forum Collections and Generics
    Replies: 2
    Last Post: September 6th, 2013, 11:36 PM
  2. Replies: 3
    Last Post: March 4th, 2013, 09:30 PM
  3. i feel like a retard! Java noob
    By javanab in forum The Cafe
    Replies: 4
    Last Post: November 9th, 2012, 09:33 PM
  4. [SOLVED] Instance data member vs Local variables (primitive/object reference)
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2011, 12:42 AM
  5. Noob question: Why .class?
    By Shaybay92 in forum Java Theory & Questions
    Replies: 10
    Last Post: August 22nd, 2011, 04:56 AM