Results 1 to 5 of 5
  1. #1
    Skater901 is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Using variables from a non-instantiated class

    Hi there.

    I'm working on a Uni assignment which is to make a simple game. A library class has been provided for me to use, called IceCreamBot. Inside this class are a few public final variables, and my lecturer made mention of being able to use them in the organiser class that we students have to write. However, there's a problem with this that he also mentioned. The problem is that I cannot refer to these variables before the class has been instantiated.

    Here's my code, roughly.

    IceCreamBot gameBot = new IceCreamBot(false);

    gameBot.SUCCESS;

    When I try to compile that, it gives me an error that it doesn't know what gameBot.SUCCESS is, because the object of IceCreamBot hasn't been created yet. So my question is this: are these public final variables in the IceCreamBot class completely useless? Because I can't see how I can refer to them in my Java file if I can't refer to them before the instance of IceCreamBot is created, but I cannot compile the program to run it to create that instance!

    Cheers guys.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Using variables from a non-instantiated class

    You can refer to public final fields if they are static; the compiler uses them without loading and initializing the rest of the class for wich those fields are members; it is als a pitfall: the class containing those fields can be recompiled while other classes still use the old static final field value.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Skater901 is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: Using variables from a non-instantiated class

    Hi Jos,

    Thanks for the reply, though I already know about making variables static. The library class we are provided with is not to be modified or submitted by us students. So, NON-STATIC public final variables, can they actually be used in my Java program?

    Thanks.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Using variables from a non-instantiated class

    For a non-static public final variable you need an object of that class to access it. If there is an object, that entire class must've been loaded and initialized. There is no way around it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Skater901 is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: Using variables from a non-instantiated class

    Haha, never mind, I'm a total idiot. I got the name wrong of the variable I was trying to refer to, and that's why there was an error. Changing that, my Java file now compiles.

    Thanks for the help, even though it wasn't needed because I'm an idiot. :p

Similar Threads

  1. Class can't be instantiated.
    By 3maksim in forum Java Applets
    Replies: 12
    Last Post: 01-25-2012, 05:23 AM
  2. Variables from another class won't assign to main class
    By erthbound0 in forum New To Java
    Replies: 3
    Last Post: 12-15-2011, 10:07 PM
  3. class is abstract; cannot be instantiated
    By Jamison5213 in forum New To Java
    Replies: 3
    Last Post: 04-25-2010, 09:16 PM
  4. Replies: 1
    Last Post: 01-03-2010, 09:48 PM
  5. Replies: 17
    Last Post: 07-12-2009, 06:42 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
  •