Questions tagged [inner-classes]

In object-oriented programming (OOP), an inner class or nested class is a class declared entirely within the body of another class or interface. However in Java, an inner class is a non-static nested class.

Inner classes

In object-oriented programming (OOP), an inner class or nested class is a class declared entirely within the body of another class or interface. However in Java, an inner class is a non-static nested class.

Resources

2328 questions
128
votes
6 answers

Is it possible to make anonymous inner classes in Java static?

In Java, nested classes can be either static or not. If they are static, they do not contain a reference to the pointer of the containing instance (they are also not called inner classes anymore, they are called nested classes). Forgetting to make…
Thilo
  • 257,207
  • 101
  • 511
  • 656
128
votes
12 answers

How can I include raw JSON in an object using Jackson?

I am trying to include raw JSON inside a Java object when the object is (de)serialized using Jackson. In order to test this functionality, I wrote the following test: public static class Pojo { public String foo; @JsonRawValue public…
bhilstrom
  • 1,471
  • 2
  • 11
  • 8
122
votes
7 answers

Nested classes' scope?

I'm trying to understand scope in nested classes in Python. Here is my example code: class OuterClass: outer_var = 1 class InnerClass: inner_var = outer_var The creation of class does not complete and I get the error:
user214870
115
votes
13 answers

Inner class within Interface

Is it possible to create an inner class within an interface? If it is possible why would we want to create an inner class like that since we are not going to create any interface objects? Do these inner classes help in any development process?
gmhk
  • 15,598
  • 27
  • 89
  • 112
91
votes
12 answers

Why does Java prohibit static fields in inner classes?

class OuterClass { class InnerClass { static int i = 100; // compile error static void f() { } // compile error } } Although it's not possible to access the static field with OuterClass.InnerClass.i, if I want to record something that should…
Jichao
  • 40,341
  • 47
  • 125
  • 198
85
votes
6 answers

Test cases in inner classes with JUnit

I read about Structuring Unit Tests with having a test class per class and an inner class per method. Figured that seemed like a handy way to organize the tests, so I tried it in our Java project. However, the tests in the inner classes doesn't seem…
Svish
  • 152,914
  • 173
  • 462
  • 620
85
votes
7 answers

Can we create an instance of an interface in Java?

Is it possible to create an instance of an interface in Java? Somewhere I have read that using inner anonymous class we can do it as shown below: interface Test { public void wish(); } class Main { public static void main(String[] args) { …
Ninja
  • 1,166
  • 2
  • 9
  • 16
80
votes
10 answers

When to use inner classes in Java for helper classes

If I have for example a class along with a helper class to do some of its functionality, does it make sense to make it as an inner class. public class Foo { private FooHelper helper; // constructor & any other logic …
Hild
  • 2,625
  • 3
  • 22
  • 22
79
votes
4 answers

Private inner classes in C# - why aren't they used more often?

I am relatively new to C# and each time I begin to work on a C# project (I only worked on nearly mature projects in C#) I wonder why there are no inner classes? Maybe I don't understand their goal. To me, inner classes -- at least private inner…
Sylvain Rodrigue
  • 4,751
  • 5
  • 53
  • 67
77
votes
5 answers

How can "this" of the outer class be accessed from an inner class?

Is it possible to get a reference to this from within a Java inner class? i.e. class Outer { void aMethod() { NewClass newClass = new NewClass() { void bMethod() { // How to I get access to "this" (pointing to outer) from…
llm
  • 5,539
  • 12
  • 36
  • 30
75
votes
4 answers

Why can't I create an enum in an inner class in Java?

What I try to do is this: public class History { public class State { public enum StateType { Eclipse gives me this compile error on StateType: The member enum StateType must be defined inside a static member type. The error disappears…
Steven Roose
  • 2,731
  • 4
  • 29
  • 46
74
votes
9 answers

What's the best way of accessing field in the enclosing class from the nested class?

Say if I have a dropdown in a form and I have another nested class inside of this class . Now what's the best way to access this dropdown from the nested class?
Learner
71
votes
3 answers

How does "object.new" work? (Does Java have a .new operator?)

I came across this code today whilst reading Accelerated GWT (Gupta) - page 151. public static void getListOfBooks(String category, BookStore bookStore) { serviceInstance.getBooks(category, bookStore.new BookListUpdaterCallback()); } public…
chickeninabiscuit
  • 9,061
  • 12
  • 50
  • 56
66
votes
5 answers

Why inner class can override private final method?

I wondered if it makes sense to declare a private method as final as well, and I thought it doesn't make sense. But I imagined there's an exclusive situation and wrote the code to figure it out: public class Boom { private void touchMe() { …
chicout
  • 937
  • 7
  • 16
65
votes
5 answers

Why Java inner classes require "final" outer instance variables?

final JTextField jtfContent = new JTextField(); btnOK.addActionListener(new java.awt.event.ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent event){ jtfContent.setText("I am OK"); } } ); If I omit final, I see…
Adham shafik
  • 1,044
  • 1
  • 13
  • 16