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
1
vote
1 answer

What is the right lambda for this inner class.SonarLint suggest me to "Make this anonymous inner class a lambda"

return new KeyGenerator() { @Override public Object generate(Object object, Method method, Object... objects) { String username = null; try { username = objectStorageService.getAuthDetail().getUsername(); …
1
vote
1 answer

Implicit constructor in case of anonymous class whose super class is an Inner Class

Consider the following article from the JLS: §15.9.5.1 When the anonymous class extends an inner class - then for the implicit constructor of the anonymous class - following is the rule regarding the body of the implicit constructor: The…
1
vote
1 answer

Inner class problem - Java unable to find "InnerClass2" when Nested within Main

Noob here. I have tried the following code, but it appears that Java cannot find InnerClass2 when nested. I could not find any guide on how to fix this issue. I also tried InnerClass2 in = Main.new InnerClass2(); but it did not work either.…
uweo030
  • 33
  • 4
1
vote
1 answer

How to call member variable in parent of outerClass from annonymous InnerClass

I have OuterClass extends ParentClass. I'm trying to override annonymous InnerClass of ParentClass in OuterClass, and what I want to do is to call the member variable of ParentClass inside the overrided annonymous InnerClass. Here is my code: class…
1
vote
2 answers

Java anonymous inner class declaration

How to define the anonymous inner class separately from the constructor block? For example in my codes I want method2 do the same thing as method1 The only difference is method2 uses the Factory class to create the ClosableResultSet. import…
len
  • 376
  • 2
  • 19
1
vote
4 answers

Why does java allow class level variables to be reassigned in anonymous inner class, whereas same is not allowed for local variables

This question is similar to Lambdas: local variables need final, instance variables don't,but the only difference is this question is valid even without lambda expressions i.e. valid even on Java7. Here is the code snippet below. public class…
1
vote
3 answers

Use of anonymous class in sort method

why if I put an anonymous class with Comparator in the sort method of List the compiler show me an error? My code: public class Example2 { public static void main(String[] args) { List l = Arrays.asList("a","b","c","d"); …
Sam
  • 536
  • 5
  • 23
1
vote
2 answers

Anonymous inner class in C++ (Java-style listener)

My C/C++ skills are a bit rusty, and I've mostly been working in Java for the past few years. Now I just started playing around with Arduino, and made a simple button class. I want to add an event listener, so I did something like this: class…
Magnus
  • 17,157
  • 19
  • 104
  • 189
1
vote
1 answer

Getting error when instantiating a trait within a class (method became private)

In the following code, I'm trying to instantiate the trait A with the Decorator class and add a method p to it so I can get another object A but with a p method: trait A { def x: Int } case class Decorator(a: A) { def withPrint: A = new A { …
1
vote
1 answer

How to access anonymous inner class in main method?

How to access the anonymous inner class object in main method. It is giving compile time error saying that "cannot make static reference to non static method". If I am making anonymous inner class as static then I can access ut I want to access…
Ankit
  • 95
  • 1
  • 9
1
vote
1 answer

Variable and access in anonymous inner-class

I have some question about anonymous inner-class. I just realize that in my anonymous inner-class can refer to two variable with the same name to outter class and in ineer-class it self, How do I know that what it refer to at that moment. This is…
1
vote
1 answer

Anonymous Class - Can we have two new objects for a same anonymous class?

When we create an anonymous class, like Employee emp = new Employee() { void get() { //Some body } void put() { //Some body } }; emp.set(); emp.get(); the object reference emp refers to the object of the above anonymous inner…
Ishwar
  • 338
  • 2
  • 11
1
vote
2 answers

Anonymous inner classes as keys in Java, but what in C#?

In Wicket, they have something called a MetaDataKey. These are used to store typed meta information in Wicket components. Since Wicket makes heavy use of serialization, the Wicket designers decided that simple object identity would not be reliable…
cdmckay
  • 31,832
  • 25
  • 83
  • 114
1
vote
3 answers

method reference Java using an inner class

public static class CompareClass { public static int CompareBetweenStudents(Student student, Student othStudent) { return student.getNume().compareTo(othStudent.getNume()); } } public List filterStudentsByLetter(String…
1
vote
3 answers

What is happening while instantiating an abstract class? What is an anonymous inner class?

What is happening while instantiating class Person? What is an anonymous inner class? abstract class Person { abstract void eat(); } class TestAnonymousInner { public static void main(String args[]) { Person p = new Person() { …
user6091735