Talk:Program name: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 4: | Line 4: | ||
<lang java>public class Test { |
<lang java>public class Test { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
Class c = new Object().getClass().getEnclosingClass(); |
Class c = new Object(){}.getClass().getEnclosingClass(); |
||
System.out.println(c.getName()); |
System.out.println(c.getName()); |
||
} |
} |
||
}</lang> |
}</lang> |
||
:That doesn't work. There is no enclosing class for Object so it's an NPE when you try to get the name. I'll correct the example. --[[User:Mwn3d|Mwn3d]] 04:32, 6 August 2011 (UTC) |
:That doesn't work. There is no enclosing class for Object so it's an NPE when you try to get the name. I'll correct the example. --[[User:Mwn3d|Mwn3d]] 04:32, 6 August 2011 (UTC) |
||
:: Strange. It printed 'Test' yesterday when I tried it. But maybe I made a change to the code after that. I've added a body to new Object, now it works again. Anyway, the System.getProperty thing is even better. [[User:Fwend|Fwend]] 07:54, 6 August 2011 (UTC) |
|||
:I agree, it's pointless to hardcode the class and then ask for it as a string. Thanks Fwend and Mwn3d for correcting the example. --[[User:Mcandre]] |
:I agree, it's pointless to hardcode the class and then ask for it as a string. Thanks Fwend and Mwn3d for correcting the example. --[[User:Mcandre]] |
||