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
60
votes
13 answers

What are the purposes of inner classes

I am reviewing the concept of inner classes in java. so far from what I've understood and applied java inner classes has a link or access to the methods and fields of its outer/ enclosing class. My Question: When should create or define an inner…
user962206
  • 15,637
  • 61
  • 177
  • 270
58
votes
10 answers

Should I use nested classes in this case?

I am working on a collection of classes used for video playback and recording. I have one main class which acts like the public interface, with methods like play(), stop(), pause(), record() etc... Then I have workhorse classes that do the video…
JimDaniel
  • 12,513
  • 8
  • 61
  • 67
57
votes
1 answer

Intellij Extract Inner Class

How does one who is using IntelliJ or Android Studio extract a public static inner class to create a new top level class? Thank you.
helsont
  • 4,347
  • 4
  • 23
  • 28
56
votes
3 answers

Instantiating inner class

I working on a sample problem of over-ridding hashCode and equals method but getting an error: "No enclosing instance of type CustomHashCodeExample is accessible. Must qualify the allocation with an enclosing instance of type CustomHashCodeExample…
t0mcat
  • 5,511
  • 19
  • 45
  • 58
52
votes
3 answers

Why can nested child classes access private members of their parent class, but grandchildren cannot?

Probably similar to the question, Why can outer Java classes access inner class private members? or Access to superclass private fields using the super keyword in a subclass . But there are some differences: the children class can access the private…
andyf
  • 3,262
  • 3
  • 23
  • 37
47
votes
2 answers

Where to put inner classes?

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…
Jason Noack
  • 475
  • 1
  • 4
  • 6
45
votes
6 answers

non-static class cannot be referenced from a static context

Possible Duplicate: Why do I get “non-static variable this cannot be referenced from a static context”? Here are the codes public class Stack { private class Node{ ... } ... public static void main(String[] args){ …
Bin
  • 980
  • 2
  • 12
  • 24
44
votes
6 answers

Method local inner class vs inner class

The below code produces the output middle. Can anyone explain in detail how this is happening? Is it because the declaration of "inner" version of class A comes after the instance of class A is created in the go() method? class A { void m() { …
Ram Patra
  • 16,266
  • 13
  • 66
  • 81
44
votes
3 answers

Instantiate private inner class with java reflection

Is it possible to instantiate a private inner class from another class using Java reflection. For example if I took this code public class Main { public static void main(String[] args) {} } class OtherClass { private class Test {} } is it…
Markovian8261
  • 909
  • 4
  • 10
  • 23
43
votes
6 answers

Strange syntax for instantiating an inner class

I didn't imagine that I would encounter radically new syntax in Java anymore at this stage, but lo and behold, I just encountered something: The exact context and what the code below should do is pretty irrelevant - it's there just to give some kind…
Henrik Paul
  • 66,919
  • 31
  • 85
  • 96
40
votes
3 answers

How can I resolve "an enclosing instance that contains X.Y is required"?

I am developing a small desktop application in Netbeans. This is my first program and I am facing a very strange type of error. I know I did some thing wrong but unable to trace what I am doing wrong :( Please help me in resolving this…
Jame
  • 21,150
  • 37
  • 80
  • 107
39
votes
1 answer

Nested Class Definition in source file

If I have a nested class like so: class MyClass { class NestedClass { public: // nested class members AND definitions here }; // main class members here }; Currently, the definitions of MyClass are in the CPP file…
Samaursa
  • 16,527
  • 21
  • 89
  • 160
39
votes
3 answers

Creating an instance of a nested class in XAML

in a XAML file (a WPF UserControl), is there a way to reference an inner class "B" defined in another class "A" ? public class A { public class B { } } Something like : This syntax does not work because "B" is…
Pragmateek
  • 13,174
  • 9
  • 74
  • 108
37
votes
5 answers

Static inner classes in scala

What is the analog in Scala of doing this in Java: public class Outer { private Inner inner; public static class Inner { } public Inner getInner() { return inner; } } I specifically want my inner class to not have to have a fully…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
36
votes
3 answers

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

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…
Leo91
  • 1,741
  • 3
  • 13
  • 20