Results 1 to 3 of 3
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Creating a new non-static inner class outside of parent class

    I have an Abstract Class called GameColorEffect which contains a number of non-static Inner Classes that extend their Parent Class, GameColorEffect. I want to be able to create instances of the Inner Classes, however my IDE, eclipse, prompts me with the error:

    No enclosing instance of type GameColorEffect is accessible. Must qualify the allocation with an enclosing instance of type GameColorEffect

    And eclipse shows me a possible solution which is to turn the Inner Classes to static, this would allow me to create instances, but not really. This is because using methods from the static Inner Classes that change values in the Inner Classes will do this for every instance of the same Inner Class which is literally like a single instance. However, I want these Inner Classes to be individual with their values and still be able to use them outside as instances. I've found out a possible solution, which I'm not sure works like I want it to:

    Java Code:
     GameColorEffect = new GameColorEffect.ExampleEffect();
    However, this is in-compact because sometimes all I need is to use just a method like:

    Java Code:
     new GameColorEffect.ExampleEffect(intensity).applyEffect()
    And another solution that I already knew prior was that I could make the Inner Classes proper classes not inside of the GameColorEffect class, but this is also in-compact because I will have to have so many classes for the so many effects that I have.

    Does anyone know how I could solve this or know of a good workaround? Thanks! :)
    Regards
    augustas656

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Creating a new non-static inner class outside of parent class

    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Creating a new non-static inner class outside of parent class

    Quote Originally Posted by augustas656 View Post
    And eclipse shows me a possible solution which is to turn the Inner Classes to static, this would allow me to create instances, but not really. This is because using methods from the static Inner Classes that change values in the Inner Classes will do this for every instance of the same Inner Class which is literally like a single instance.
    I think you need to read up on inner classes. What you said above is not true. Instances of inner classes, static or non-static are unique and their instance fields are not shared between instances.

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

Similar Threads

  1. Replies: 8
    Last Post: 04-18-2012, 04:58 PM
  2. substract Parent class object from child class
    By nikosv in forum New To Java
    Replies: 0
    Last Post: 12-08-2010, 12:30 AM
  3. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  4. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  5. Replies: 7
    Last Post: 05-06-2009, 12: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
  •