Questions tagged [anonymous-class]

An anonymous class is a local class without a name. An anonymous class is defined and instantiated in a single succinct expression using the new operator.

An anonymous class is a local class without a name. An anonymous class is defined and instantiated in a single succinct expression using the new operator. While a local class definition is a statement in a block of code, an anonymous class definition is an expression, which means that it can be included as part of a larger expression, such as a method call. When a local class is used only once, consider using anonymous class syntax, which places the definition and use of the class in exactly the same place.

699 questions
-2
votes
2 answers

java anonymous class as parameter with new methods

When I search for the method by reflection it shows the newly provided method. But I don't know how to invoke that method, if someone has got any idea how to do it please tell me. //some pakage pakage xyz; class A { // a simple method of class…
Ankur
  • 29
  • 1
  • 10
-2
votes
2 answers

Accessing loop variable within anonymous class

The error that I am getting is that within public void pressed, the variable i cannot be accessed unless the int is set as final. But, if I set it as final, the i++ becomes the error. I am stuck unless I can access the i variable from within my…
-2
votes
1 answer

Java: Why anonymous inner classes can access methods of outer class?

Lately I've found out that this works, although I'd expect it not to: class Outer { Button b; void foo() {} void bar() { b = new Button(); b.setOnClickListener(new OnClickListener(){ @Override …
Alex Jenter
  • 4,324
  • 4
  • 36
  • 61
-3
votes
1 answer

Can we create an anonymous inner class from a concrete class?

There is something surprising in the following code. The ListCell class is not abstract, but we were able to create an anonymous class from it and override its method! How is this possible? ListCell listCell = new ListCell<>() { …
vorxd
  • 15
  • 3
-3
votes
1 answer

How to create an anonymous class using a method that returns a reference to a class

I want to create an anonymous class using a method that returns an instance of a class class FirstClass { public FirstClass() { System.out.println("First class created"); } } class SecondClass { public SecondClass() { …
-3
votes
3 answers

Can't understand use of this keyword in View.setOnClickListner(this)?

I know that keyword this refers to current instance of class. But When we implement View.OnClickListener in our class then on calling method textview.setOnClickListener(this), How does argument this(instance object of class) of…
Novice
  • 49
  • 11
-3
votes
3 answers

Compilation error in anonymous class

I am working on anonymous classes and while working i come up with the case where i am unable to call a method using anonymous class. I am getting following compilation error at m1() The method m1(int) in the type new I(){} is not applicable for…
T-Bag
  • 10,916
  • 3
  • 54
  • 118
-3
votes
2 answers

What kind of feature is that?

I could not understand following code snippet: Unirest.setObjectMapper(new ObjectMapper() { private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper = new com.fasterxml.jackson.databind.ObjectMapper(); public
softshipper
  • 32,463
  • 51
  • 192
  • 400
-5
votes
5 answers

Java Anonymous Class - minimal example

I'm trying to understand Java anonymous classes. Looking here: https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html And here: http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm I understand the basic syntax, but the…
bigcodeszzer
  • 916
  • 1
  • 8
  • 27
1 2 3
46
47