{"id":5337,"date":"2014-03-30T20:51:03","date_gmt":"2014-03-30T20:51:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-inner-classes-collection-of-common-programming-errors\/"},"modified":"2014-03-30T20:51:03","modified_gmt":"2014-03-30T20:51:03","slug":"problem-about-inner-classes-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-inner-classes-collection-of-common-programming-errors\/","title":{"rendered":"problem about inner-classes-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/15e7a6e4a98126f1d11435ba77112214?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nEric B.<br \/>\njava reflection field inner-classes<br \/>\nI&#8217;m running into a strange result here and am not sure if it is a bug in Java or it is expected behaviour. I have an inner class on which I&#8217;ve used reflection to get the declared fields (class.getDeclaredFields()). However, when I loop over the list of fields and check the individual types, the &#8220;this&#8221; field returns the outerclass and not the inner class.Is this expected behaviour? It seems quite odd to me.Ex:import java.lang.reflect.Field;public class OuterClass {public class InnerClass{publi<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/f9d29bfce8b8c3e57e7f5b7555706bad?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\njohnny<br \/>\njava inner-classes<br \/>\nAre inner classes commonly used in Java? Are these the same as nested classes? Or have these been replaced in Java by something better? I have a book on version 5 and it has an example using an inner class, but I thought I read somewere that inner classes were &#8220;bad.&#8221;I have no idea and was hoping for thoughts on it.Thank you.<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7df9b99b5d8ac9eda9330768ca1acf09?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nbestsss<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/a2583e479ced3d41830d7d82f2f52478?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nKLibby<br \/>\njava memory-management jvm inner-classes callstack<br \/>\nIn the following code:public class Main {Emp globalEmp;public void aMethod(){final int stackVar = 10;globalEmp = new Emp(){public void doSomeThing(){System.out.println(&#8220;stackVar :&#8221; + stackVar);}};}public static void main(String[] args){Main m = new Main();m.aMethod();m.globalEmp.doSomeThing();} }interface Emp{public void doSomeThing(); }As I can understand the following will be executed:Main m = new Main(); : A new Instance of the Main class will be created, with globalEmp set to null. m.aMethod<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/346348adcb3b4e7ed14d3024c8827a38?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nmartsraits<br \/>\njava reflection visibility inner-classes<br \/>\nI have two compilation units:public class OuterClass{private static class InnerClass{public String test(){return &#8220;testing123&#8221;;}}public static void main( String[] args ){new CallingClass().test( new InnerClass() );} }public class CallingClass{public void test( Object o ){try{Method m = o.getClass().getMethod( &#8220;test&#8221; );Object response = m.invoke( o );System.out.println( &#8220;response: &#8221; + response );}catch( Exception e ){e.printStackTrace();}} }If they are in the same package, everything works and &#8220;re<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5e3aff615b72ea6efe4a35bc16159a2c?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nErik Schierboom<br \/>\njava inner-classes<br \/>\nfinal JTextField jtfContent = new JTextField(); btnOK.addActionListener(new java.awt.event.ActionListener(){public void actionPerformed(java.awt.event.ActionEvent event){jtfContent.setText(&#8220;I am OK&#8221;);} } );If I omit final, I see the error &#8220;Cannot refer to a non-final variable jtfContent inside an inner class defined in a different method&#8221;.Why must an anonymous inner class require the outer classes instance variable to be final in order to access it?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/540f9991525c193eaf5ad4f8b683a637?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nValtteri<br \/>\njava swing gui inner-classes<br \/>\nSome time ago I wrote a little image viewer\/processing program with Java, a mini-Photoshop, if you will. I wanted there to be a drop-down menu where I could select which one of the images I have opened would be &#8220;on the table&#8221;, ie. shown and methods applied to. I wanted the name of the image to be the name of the JMenuItem shown in the menu. I also wanted a new button to appear when I add a new image.I wondered this for some time and finally produced this solution, a new class that handles the<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/46584628e6299f53d0eb90677c99aa4b?s=32&amp;d=identicon&amp;r=PG&amp;f=1\" \/><br \/>\nferrerverck<br \/>\njava enums inner-classes instance-variables<br \/>\nAs I understood inner enums are always explicitly ?r implicitly static in java. Which means I can&#8217;t access instance fields from my inner enum class.public class InnerEnum {private enum SomeInnerEnum {VALUE1() {@Overridepublic void doSomething() {\/\/ ERROR: WON&#8217;T COMPILE\/\/ Cannot make static reference\/\/ to non-static field iSystem.out.println(i);}},VALUE2() {@Overridepublic void doSomething() {\/\/ do something else with i }};public abstract void doSomething();}private int i = 10; }I have found it p<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/doLQL.jpg?s=32&amp;g=1\" \/><br \/>\ndogbane<br \/>\njava generics casting inner-classes<br \/>\nConsider the following code:public class Outer&lt;T&gt; {public class Inner{}public static &lt;T&gt; Outer&lt;T&gt;.Inner get(){Object o = new Object();return (Outer&lt;T&gt;.Inner)o;}public static void main(String[] args) throws Exception {Outer.&lt;String&gt;get();} }This code compiles successfully in Eclipse, but fails to compile in javac:Outer.java:10: &#8216;)&#8217; expectedreturn (Outer&lt;T&gt;.Inner)o;^ Outer.java:10: &#8216;;&#8217; expectedreturn (Outer&lt;T&gt;.Inner)o;^ Outer.java:10: illegal start of exp<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/48a7eb616b5d185305fb9aed9a653dce?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nBrian<br \/>\njava inheritance inner-classes private-methods<br \/>\nLet&#8217;s take the following code:public class Test {class A {public A() {}private void testMethod() {System.out.println(&#8220;A&#8221;);}}class B extends A {public B() { super(); }private void testMethod() {System.out.println(&#8220;B&#8221;);}}public Test() { }public A get() {return new B();}public static void main(String[] args) {new Test().get().testMethod();} }I would expect that code to write B. A is written instead.It may feel weird (at least to me) the fact that a class can call private methods of the inner classe<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7e76d09b24c13d36f3e7b86fd569f601?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nEto Demerzel<br \/>\njava json serialization inner-classes jackson<br \/>\nI have a question concerning Json deserialization using Jackson. I would like to deserialize a Json file using a class like this one: (taken from http:\/\/wiki.fasterxml.com\/JacksonInFiveMinutes)public class User {public enum Gender { MALE, FEMALE };public static class Name {private String _first, _last;public String getFirst() { return _first; }public String getLast() { return _last; }public void setFirst(String s) { _first = s; }public void setLast(String s) { _last = s; }}private Gender _gende<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/b91eb3e49dda35f15510cfac364055c2?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\njava.is.for.desktop<br \/>\njava reflection inner-classes anonymous-class<br \/>\nI have an anonymous inner class inside another class (SomeClass).Both SomeClass.class.getClasses() and SomeClass.class.getDeclaredClasses() return empty arrays.I couldn&#8217;t find some hints on this in Class&#8217; Javadocs.Can anonymous inner classes be retrieved using reflection in some way?What else are notable differences between anonymous inner classes and normal inner classes?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/2e29549039f449f3725a13903f8daf73?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nperez<br \/>\njava inner-classes instanceof<br \/>\nI coded in NetBeans something like this:public class Grafo&lt;V, E&gt; {class Par{int a, b;Par(int a, int b) {this.a = a;this.b = b;}@Overridepublic boolean equals(Object ob){if(ob instanceof Par) {Par p = (Par)ob;return this.a==p.a &amp;&amp; this.b==p.b;}return false;}}\/\/stuff&#8230; } \/\/end of class GrafoThe error is in the method equals() from inner class &#8220;Par&#8221;.NetBeans says that the error is &#8220;illegal generic type of instanceof&#8221;. The error is in the line below.if(ob instanceof Par) {What is the<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/a37b0d1063d59f32457522744bbc8e56?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ncrazy horse<br \/>\njava inner-classes static-initialization<br \/>\nContext: java.io.File class has a static inner class method as follows:LazyInitialization.temporaryDirectory();[EDITED to add some more code] My code below eventually calls the above line of code. An exception is thrown from within the temporaryDirectory() method, which in my context is fine\/expected.try {File tempFile = File.createTempFile(&#8220;aaa&#8221;, &#8220;aaa&#8221;); } catch (Exception e) {\/\/ handle exception }Then, when I next invoke the same method (createTempFile) again, I get a &#8220;java.lang.NoClassDefFou<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/ce4dc45e23bedfbd094b55c796c22e88?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ntoriscope<br \/>\njava runtime classloader inner-classes class-loading<br \/>\nI have a program where I compile java code that somebody writes in a text box, and run it. They type out the full source code, class and allI save the class they write to a random java source file, and then compile and load the class through a classloader. This works perfectly.I have a new issue though, that of sub classes. I give the outer class a unique name, and load that class.Ex.TEMP1110.java -&gt; TEMP1110.class, etc. With inner classes, it compiles to TEMP1110$InnerClass.class I try loadi<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/cfa7503904d84163ade8d8754e163e38?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nLeoa<br \/>\nandroid static context inner-classes android-gcm<br \/>\nI&#8217;m trying to get the context of FragmentStackSupport Activity and use it in an inner static class. I&#8217;ve instantiated FragmentStackSupport in the inner static class and I&#8217;m using getBaseContext() to get the context of FragmentStackSupport. Putting the outer class context inside GCMRegistar.checkDevice(thisContext) does not give an error in the code but crashes the application. I can&#8217;t use &#8216;this&#8217; or FragmentStackSupport.this because the inner class is static. &#8220;this&#8221; would work if the class was pu<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7cb1eb3467901192d2301afd83d03bc9?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nuser881667<br \/>\njava android android-asynctask inner-classes<br \/>\nI hope I explained this issue correctly in the title. I have some inner classes that extend AsyncTask to read some JSON data and parse it. I have two separate ones for each search query the user makes. Once I have the two separate results I want to do stuff with them in the same method. My thought was to use a variable argument method and in the onPostExecute of each individual AsyncTask send the result to that method. Not sure if this is the right way to go about it as the app is crashing on se<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/4c54fc273f09f80c77d17f2311f91d6a?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJonathan Edwards<br \/>\nphp class mysqli inner-classes prepare<br \/>\nthere has been a similar topic (How to access mysqli connection in another class on another page?) but it doesn&#8217;t quite answer my question, or I&#8217;m missing the point. I&#8217;m trying to build a class which will allow me to quickly build the bones of a slideshow by pulling a set of image urls and div ids from a database. Here&#8217;s what I&#8217;ve managed:class make_slide {private $slide_mysqli;public $get_slide;public $single_slide;public $get_imgurl1; public $get_imgurl2; public $get_imgurl3; public $get_img<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/c0c26698d174c9c2ebc5d318636d353b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nuser1494396<br \/>\njava inner-classes scriptengine<br \/>\nI have a class that I can&#8217;t change as follows:public class Foo {public final int ID=0;public int bar;public final Object baz=null; }I want to have an anonymous inner class that overrides Foo such as:public Foo newFoo(final int mID, final int mbar, final Object mbaz) {return new Foo(){public final int ID = mID;public int bar = mbar;public final Object baz = mbaz;}; }I then have a javax.script.ScriptEngine that I want to call something like this in JS: newFoo(0,0,undefined)[&#8220;ID&#8221;]My problem is that<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/q4wwH.jpg?s=32&amp;g=1\" \/><br \/>\nThe New Idiot<br \/>\njava inner-classes<br \/>\nWhy is the following piece of code not working? import java.util.Comparator;public class TestInner {public static void main(String[] args) {Comparator&lt;String&gt; comp = new Comparator&lt;String&gt;(){private String sample = null;@Overridepublic int compare(String arg0, String arg1) {\/\/ TODO Auto-generated method stubreturn arg0.compareTo(arg1);}public void setText(String t1){sample = t1;}};\/\/ compiler error &#8211; Method is undefined for the type Comparator&lt;String&gt; comp.setText(&#8220;xyz&#8221;); }}I h<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/687f423fcc59d1e15115abddaefa0c49?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nKen<br \/>\njava javascript subclass rhino inner-classes<br \/>\nI&#8217;m trying to subclass an inner class (defined in Java) in Rhino, and I can&#8217;t seem to make it work.I&#8217;ve got some compiled Java code (which I essentially can&#8217;t change) that has an inner abstract class:package mypackage; class MyClass {abstract static class MyInnerClass {abstract void print(String s);} }From Rhino, I can see it just fine:js&gt; Packages.mypackage.MyClass.MyInnerClass [JavaClass mypackage.MyClass$MyInnerClass]But I can&#8217;t figure out how to subclass it. I figured something like this<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/33ca8dd44993cafe3c851c9323111987?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nrampion<br \/>\njava ruby jruby inner-classes<br \/>\nSo given the following java class:class Outer {private int x;public Outer(int x) { this.x = x; }public class Inner{private int y;public Inner(int y) { this.y = y; }public int sum() { return x + y; }} }I can create an instance of the inner class from Java in the following manner:Outer o = new Outer(1); Outer.Inner i = o.new Inner(2);However, I can&#8217;t seem how to do the same from JRuby#!\/usr\/bin\/env jruby require &#8216;java&#8217; java_import &#8216;Outer&#8217;o = Outer.new(1); i = o.Inner.new(2); #=&gt; NoMethodError:<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/12356ae4540e9286f4984eb7beef801e?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJarrod Roberson<br \/>\njava enums constructor inner-classes type-erasure<br \/>\npublic enum Days {SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY;public enum WeekDays{MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,}public enum WeekEnds{SATURDAY,SUNDAY;} }public class InnerEnumTestClass&lt;E extends Enum&lt;E&gt;&gt; {public E enumtype;\/*** @param enumtype*\/public InnerEnumTestClass(E enumtype) {super();this.enumtype = enumtype;}\/*** @param args*\/public static void main(String[] args) {InnerEnumTestClass&lt;Days&gt; testObj = new InnerEnumTestClass&lt;Days&gt;(Days.WeekDa<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/1127d0098e693682f382758abd9f6c76?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nSvante<br \/>\njava inner-classes java1.4<br \/>\nWhen passing a final object (A String in the code below) it shows up as null when printed from the anonymous inner class. However, when a final value type or straight final String is passed in, its value is correctly displayed. What does final really mean in context of the anonymous inner class and why is the object passed null?public class WeirdInners {public class InnerThing{public InnerThing(){print();}public void print(){}}public WeirdInners(){final String aString = &#8220;argh!&#8221;.toString();fina<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/50d6f954cbf5f65d85d6f093271ea2a2?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nNicola Peluchetti<br \/>\njavascript dom inner-classes javascript-objects<br \/>\nI am newbie to javascript objects and I have a problem. I am trying to make an image gallery but I keep getting an error that this.current, this.size &amp; this.initial are undefined, and therefore, the script cannot work. Please help me resolve this error. the following is the full script.function gallery() {this.image = new Array(10);this.initial = 1; this.current = 0;this.size = 10;this.frame_height = 400;this.frame_width = 600;this.initialize=function(){if(document.images){var count = 1;fo<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Eric B. java reflection field inner-classes I&#8217;m running into a strange result here and am not sure if it is a bug in Java or it is expected behaviour. I have an inner class on which I&#8217;ve used reflection to get the declared fields (class.getDeclaredFields()). However, when I loop over the list of fields and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5337","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5337"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5337\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}