inner-classes

C++ - What's the point of nested classes?

让人想犯罪 __ 提交于 2019-12-03 09:26:01
I'm studying a little of C++ and now I'm fighting against it's similitudes with Java. I know the purpose of inner classes in Java, but now I'm trying to use nested classes in C++, and I discover that private attributes of "container" class are not visibile by nested class , so why I should use them? Also, is there a way to make visibile those attributes? Adriano Repetti I'm studying a little of C++ and now I'm fighting against it's similitudes with Java. First of all be aware that C++ nested classes are similar to what in Java you call static nested classes . There isn't anything in C++ syntax

Access outer class “super” from inner class in Java

有些话、适合烂在心里 提交于 2019-12-03 08:16:53
问题 How can I access outer class' super from an inner class? I'm overriding a method to make it run on a different thread. From an inline Thread, I need to call the original method but of course just calling method() would turn into an infinite recursion. Specifically, I'm extending BufferedReader: public WaitingBufferedReader(InputStreamReader in, long waitingTime) { [..] @Override public String readLine() { Thread t= new Thread(){ public void run() { try { setMessage(WaitingBufferedReader.super

how to use nested class in another class in java?

夙愿已清 提交于 2019-12-03 07:39:04
I have some situation that I want to use inner class of another class in another class. like... public class ListData { public static class MyData { public String textSongName, textArtistName, textDuration, textDownloadPath, textSongSize, textAlbumName, textUrl; public boolean enable; public MyData(String songName, String artistName, String duration, String downloadPath, String songSize, String albumName, String url, boolean e) { textSongName = songName; textArtistName = artistName; textDuration = duration; textDownloadPath = downloadPath; textSongSize = songSize; textAlbumName = albumName;

Java Inner Class extends Outer Class

牧云@^-^@ 提交于 2019-12-03 06:55:41
There are some cases in Java where an inner class extends an outer class. For example, java.awt.geom.Arc2D.Float is an inner class of java.awt.geom.Arc2D, and also extends Arc2D. (c.f. http://download.oracle.com/javase/6/docs/api/java/awt/geom/Arc2D.Float.html ) Also, sun.org.mozilla.javascript.internal.FunctionNode.Jump extends sun.org.mozilla.javascript.internal.Node, which is a superclass of FunctionNode. (sorry... cannot find a link to the javadoc) To me, this seems odd. Could you then create these? new Arc2D.Float.Float() //n.b. I couldn't get this to compile in Intellij IDEA; new

No enclosing instance of type PerfHelper is available due to some intermediate constructor invocation

混江龙づ霸主 提交于 2019-12-03 05:38:10
Consider the below code: class abstract Normal1 extends Something { } class Outer { class abstract Inner extends Normal1 { } } class General extends Outer.Inner // Problem occurs at this { } The error I am getting is "No enclosing instance of type PerfHelper is available due to some intermediate constructor invocation" My question is can I extend the inner class like above ? Jordão Declare the inner class as static and you should be able to extend it: class outer { static abstract class inner extends normal1 { } } If inner is not abstract, it's tied to outer, and can only exist when an

Java: How to check if an object is an instance of a non-static inner class, regardless of the outer object?

前提是你 提交于 2019-12-03 05:04:54
If I have an inner class e.g. class Outer{ class Inner{} } Is there any way to check if an arbitrary Object is an instance of any Inner , regardless of its outer object? instanceof gives false when the objects are not Inner s from the same Outer . I know a workaround is just to make Inner a static class, but I'm wondering if what I'm asking is possible. Example: class Outer{ Inner inner = new Inner(); class Inner{} public boolean isInner(Object o){ return o instanceof Inner; } } Outer outer1 = new Outer(); Outer outer2 = new Outer(); boolean answer = outer1.isInner(outer2.inner); //gives false

local variable is accessed within inner class (java)

你。 提交于 2019-12-03 04:58:55
I got two errors after I compiled my code. The errors are: 1. local variable input is accessed within inner class; needs to be declared final String name = input.getText(); 2. local variable c_age is accessed within inner class; needs to be declared final Object child_age = c_age.getSelectedItem(); This is my code: import javax.swing.*; import java.awt.event.*; public class GUI { public static void main(String[] args) { JFrame frame = new JFrame("Try GUI"); JLabel l1 = new JLabel("Please Enter Your Child's Name"); JTextField input = new JTextField("",10); JLabel l2 = new JLabel("Choose Your

Where to put inner classes? [closed]

谁都会走 提交于 2019-12-03 04:12:57
Some might like to argue that this is a candidate for the least important issue of all times. Yet code style is a very important topic for me, and I want to ensure that I write code in a readable way - for me and the majority of developers. That's why I'm wondering where you guys are declaring your inner classes. I'm following the following method ordering scheme, because it is quite common: public void foo() { usedByFoo(); } private void usedByFoo() { } public void bar() { } I order them from top to bottom, every method as close to where it is used. Now I could do the same with inner classes,

What happens to a BufferedReader that doesn't get closed within a callable.call?

大兔子大兔子 提交于 2019-12-03 03:58:10
I have three questions. To explain, I was reviewing someone's code, and noticed BufferedReader s sometimes aren't being closed. Usually, Eclipse gives a warning that this is a potential memory leak (and I fix it). However, within a Callable inner class, there is no warning. class outerClass { ... public void someMethod() { Future<Integer> future = outputThreadPool.submit(new innerClass(this.myProcess.getInputStream(), threadName)); ... } class innerClass implements Callable<Integer> { private final InputStream stream; private final String prepend; innerClass(InputStream stream, String prepend)

Outer vs. Super class

徘徊边缘 提交于 2019-12-03 03:10:23
Does super has higher priority than outer class? Consider we have three classes: ClassA ClassB Anonymous class in ClassB that extends ClassA ClassA.java: public class ClassA { protected String var = "A Var"; public void foo() { System.out.println("A foo()"); } } ClassB.java: public class ClassB { private String var = "B Var"; public void test() { new ClassA() { public void test() { foo(); System.out.println(var); } }.test(); } public void foo() { System.out.println("B foo()"); } } When I call new ClassB().test() , I get the following output (which is pretty much expected): A foo() A Var