tag:blogger.com,1999:blog-2913369703414801920.post934339479004260218..comments2023-07-01T07:45:54.461-07:00Comments on Konstantin's Blog: Java Language Puzzle 2Konstantin Komissarchikhttp://www.blogger.com/profile/12487640637368516721noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-2913369703414801920.post-47142185226075467562011-06-27T20:33:23.788-07:002011-06-27T20:33:23.788-07:00I just verified that my guess was correct. The fol...I just verified that my guess was correct. The following works:<br /><br />Entity entity = Entity.class.newInstance();<br />Factory factory = Factory.class.getConstructor(Entity.class).newInstance(entity);<br />Entity otherEntity = factory.create();konberghttps://www.blogger.com/profile/04616226121996611123noreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-5131730237531830602011-06-27T15:57:41.972-07:002011-06-27T15:57:41.972-07:00Add the "static" modifier to the inner F...Add the "static" modifier to the inner Factory class.Brendanhttps://www.blogger.com/profile/02422859704501070399noreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-70574799738991540172011-06-27T15:13:51.898-07:002011-06-27T15:13:51.898-07:00Make the Factory class static so that you don'...Make the Factory class static so that you don't need an instance of Entity to create it.<br /><br />public static class Factoryjoakimehttps://www.blogger.com/profile/11919226774248421689noreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-2852725207002167702011-06-27T15:01:03.326-07:002011-06-27T15:01:03.326-07:00InstanciationException is thrown because Factory i...InstanciationException is thrown because Factory is a inner class of Entity and therefore it can only be instanciated inside a non-static method of Entity. That code can be fixed by converting Factory to a static class.Rui Figueirahttps://www.blogger.com/profile/12262110061460891054noreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-52670402884737460372011-06-27T14:49:34.509-07:002011-06-27T14:49:34.509-07:00Factory isn't a static class, needs an instanc...Factory isn't a static class, needs an instance of Entity in order to instantiate it. Possibly make it static, possibly use Constructor.newInstance() and supply an Entity.konberghttps://www.blogger.com/profile/04616226121996611123noreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-80594113841979160032011-06-27T14:31:20.612-07:002011-06-27T14:31:20.612-07:00non-static inner classes need to be instanced from...non-static inner classes need to be instanced from an instance of their outer class, since they keep a reference to the outer class in a compiler-generated field called "outer".<br />In this case, you could either:<br /><br />- Create a Factory instance from an Entity object: new Entity().new Factory()<br />- change the inner class to be static, so it doesn't require an instance of the outer class to be instantiatedCKnoreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-33169278614772443202011-06-27T13:34:51.777-07:002011-06-27T13:34:51.777-07:00package puzzle;
public class Entity
{
private...package puzzle;<br /><br />public class Entity<br />{<br /> private Entity()<br /> {<br /> }<br /> <br /> public static class Factory<br /> {<br /> public Entity create()<br /> {<br /> return new Entity();<br /> }<br /> }<br /> <br /> public static void main( String[] args ) throws Exception<br /> {<br /> Factory.class.newInstance();<br /> }<br />}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-5721960021394580762011-06-27T13:26:45.462-07:002011-06-27T13:26:45.462-07:00It's because there is no Enclosing instance of...It's because there is no Enclosing instance of type Entity. You can make Factory static, but that might not be what's desired.<br /><br />If you replace Factory.class.newInstance() with new Factory(), you can see this as a compile time error. You can use new Entity().new Factory().<br /><br />If you want to leave Factory as a non-static inner class and still use reflection to instantiate it... well I'm not sure how to do that... looking forward to the answer.Ian Bullhttps://www.blogger.com/profile/02668098567506210626noreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-17147886076040049522011-06-27T13:12:39.382-07:002011-06-27T13:12:39.382-07:00Creating an instance of Factory requires an enclos...Creating an instance of Factory requires an enclosing instance of Entity.<br /><br />Perhaps the intent was to make Factory a static inner class?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-44031392866299105942011-06-27T13:04:22.813-07:002011-06-27T13:04:22.813-07:00My guess: As the factory class is a non-static inn...My guess: As the factory class is a non-static inner class, it needs an instance of the outer class to exist. The main-method doesn't create an entity object. If you want to create a Factory instance in a "regular" way, by calling the constructor, you would need an Entity object and call the Factory constructor from there (e.g entity.new Factory()). To instantiate the inner class via reflection, I guess you have to get a suitable constructor using Factory.class.getConstructors() and get pass an instance of the outer class when calling it.Bureckhttps://www.blogger.com/profile/18042165376434826160noreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-61147276233531380812011-06-27T12:48:49.787-07:002011-06-27T12:48:49.787-07:00My guess would be that the exception is thrown bec...My guess would be that the exception is thrown because Factory is a non-static inner class and therefore a Factory instance is bound to an Entity instance. Because there is no instance in the static main method, no Factory can be created.<br /><br />The solution is simply to declare the Factory class static.Joachim Mairböckhttps://www.blogger.com/profile/02285637327109121868noreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-64511035894068167022011-06-27T12:40:52.164-07:002011-06-27T12:40:52.164-07:00Because Factory is an inner class and there's ...Because Factory is an inner class and there's no enclosing Entity instance?Comentatorhttps://www.blogger.com/profile/00443431909508124725noreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-83953138289275311752011-06-27T12:19:05.130-07:002011-06-27T12:19:05.130-07:00The new instance is missing an enclosing instance ...The new instance is missing an enclosing instance of Entity, unless Factory is declared as a static class.w-ckernoreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-63910977415446333612011-06-27T12:18:30.395-07:002011-06-27T12:18:30.395-07:00changing the factory declaration to this seems to ...changing the factory declaration to this seems to work.<br /><br />public static class Factory<br /><br />I suppose the "reason" is that the static method of "main" can only reach other static declarations. If factory where its own top-level class, then it wouldn't need static.Greg A.noreply@blogger.comtag:blogger.com,1999:blog-2913369703414801920.post-42276070545899651752011-06-27T12:15:45.019-07:002011-06-27T12:15:45.019-07:00Non-static inner class Factory is only reachable v...Non-static inner class Factory is only reachable via an enclosing instance of Entity.<br /><br />Fix by:<br />a) declaring inner class Factory as static<br />b) create Factory instance via an enclosing instance of entiy (but that would defeat the factories purpose)<br /><br /> Entity x = new Entity();<br /> x.new Factory();Anonymousnoreply@blogger.com