Questions tagged [anonymous-inner-class]

Anonymous Inner Classes are local classes that are declared and instantiated inline.

These are local classes that are declared and instantiated inline, often in the middle of an expression or as an argument of a method. They can only directly extend one class or implement one interface. They can specify arguments to the constructor of the superclass, but cannot otherwise have a constructor (however, this is not a limitation, since it can have an instance initializer block to perform any initialization).

246 questions
2
votes
1 answer

How many local method anonymous classes are created?

Consider this class: public class Activity { public ArrayList containerListener = new ArrayList(); public void metodoDiProva(int num) { final int finalNum = num; containerListener.add(new…
Giovanni Far
  • 1,623
  • 6
  • 23
  • 37
2
votes
4 answers

Throwing an exception from an anonymous inner class's constructor

How do I add "throws" to an anonymous inner class's constructor? class Foo { public Foo() throws Exception { } } Becasue this doesn't work public static void main(String[] args) { Foo x = new Foo() { @Override public Foo() throws…
Blubber
  • 1,375
  • 1
  • 15
  • 32
2
votes
1 answer

Anonymous inner class using an interface in Java

So, when looking into lambda expressions and using them to substitute anonymous inner classes for EventHandlers in Java, I came across a few anonymous inner classes that made me stop and think. For instance, when writing an anonymous inner class for…
bmcentee148
  • 594
  • 1
  • 7
  • 18
2
votes
1 answer

Is it possible to access getVal() function inside displayMsg() function?

Is it possible to access the getVal() function inside displayMsg () function? I tried to create an annonymous inner-class with function getVal() and I want to call the getVal() function inside the displayMsg() function of the…
Aravind Pilla
  • 416
  • 4
  • 14
2
votes
2 answers

Runnable Inline Class declaration - able to access outer non-final variables

In the code below, I am wondering why the run() in the inline class is able to access the outer class variable - semaphore (even though it is not declared final). private Semaphore semaphore = new Semaphore(bufferSize); private Runnable…
Hari
  • 5,057
  • 9
  • 41
  • 51
2
votes
2 answers

How do I get this parameter?

public class Test{ ClassB fieldB; public Test(ClassA instanceA){ fieldB = new ClassB(); fieldB.setOnClickListener(new OnClickListener() { @Override public void onClick() { ClassC.aStaticMethod(_____); …
nomnom
  • 1,560
  • 5
  • 17
  • 31
2
votes
0 answers

How to return object from an anonymous inner class in java

If I have the following function : public class Product { public String barCode; public String name; public Category category; public double price; public Store store; WebService ws = new WebService(); public…
Adham
  • 63,550
  • 98
  • 229
  • 344
2
votes
2 answers

Class Name for Java anonymous class

Class A{ public void test(){ B b = new B(); System.out.println( "Class Name: " + b.createClassC().getClass() ); } } Class B{ public C createClassC(){ C c = new C(){ @Override public boolean equals( Object other ){ …
broun
  • 2,483
  • 5
  • 40
  • 55
2
votes
3 answers

How can I access enclosing class instance variables from inside the anonymous class?

How do I access instance variables from inside the anonymous class's method ? class Tester extends JFrame { private JButton button; private JLabel label; //..some more public Tester() { function(); // CALL FUNCTION } …
saplingPro
  • 20,769
  • 53
  • 137
  • 195
2
votes
4 answers

Instantiating a Generic Interface

I have an interface public interface Foo { public void bar(String s, T t); } I want to write a method public void baz() { String hi = "Hello"; String bye = "Bye"; Foo foo = new Foo() { public void bar(String s,…
cdk
  • 6,698
  • 24
  • 51
2
votes
3 answers

How do you resolve a circular dependency with an inner class?

(Java question) If I reference a field in an inner class, does this cause a circular dependency between the enclosing class and the inner class? How can I avoid this? Here is an example: public class Outer { private Other o; private Inner i; …
j.davies
  • 2,321
  • 1
  • 19
  • 24
2
votes
2 answers

Threaded Java server with inner class and final variable

I've written the following code to implement a Threaded Server: ServerSocket passiveSocket = new ServerSocket(port, maxConnections); while(true){ final Socket socket = passiveSocket.accept(); new Thread(new Runnable() { …
user306708
  • 921
  • 1
  • 9
  • 12
2
votes
1 answer

General Architecture for Offline sync Android apps

I am building a POS like Android app which needs to work even if Data connectivity is temporarily lost. The idea is to report all transactions to the server immediately if Data connectivity is available. If not, store it locally in SQLite and sync…
navjotk
  • 818
  • 1
  • 9
  • 15
1
vote
4 answers

How to use Outer Method's input in Anonymous Inner Class?

For Instance how can I use the input 'hasTypedSomeToken' in my Anonymou inner class in the following - public class Login { void display(boolean hasTypedSomeToken) { //some code here Button btnLogIn =…
abhihello123
  • 1,668
  • 1
  • 22
  • 38
1
vote
2 answers

The Anonymous Class Conundrum

I think I understand the basics of Anonymous classes but I'd like to clarify something. when I have a syntax such as this class A { class AnonymousClass1 Implements ActionListener{} } class A { public A() { JButton btn =…
Lews Therin
  • 10,907
  • 4
  • 48
  • 72