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

2112 questions
1869
votes
27 answers

Java inner class and static nested class

What is the main difference between an inner class and a static nested class in Java? Does design / implementation play a role in choosing one of these?
Omnipotent
  • 25,231
  • 11
  • 28
  • 33
385
votes
11 answers

Is not an enclosing class Java

I'm trying to make a Tetris game and I'm getting the compiler error Shape is not an enclosing class when I try to create an object public class Test { public static void main(String[] args) { Shape s = new Shapes.ZShape(); …
V Sebi
  • 3,973
  • 2
  • 13
  • 8
369
votes
8 answers

Java: Static vs inner class

What is the difference between static and non-static nested class?
Abhishek Sanghvi
  • 4,412
  • 6
  • 25
  • 34
363
votes
14 answers

Difference between final and effectively final

I'm playing with lambdas in Java 8 and I came across warning local variables referenced from a lambda expression must be final or effectively final. I know that when I use variables inside anonymous class they must be final in outer class, but still…
alex
  • 9,184
  • 12
  • 64
  • 86
336
votes
3 answers

When exactly is it leak safe to use (anonymous) inner classes?

I have been reading some articles on memory leaks in Android and watched this interesting video from Google I/O on the subject. Still, I don't fully understand the concept, and especially when it is safe or dangerous to user inner classes inside an…
Sébastien
  • 11,400
  • 8
  • 46
  • 63
331
votes
7 answers

Java - No enclosing instance of type Foo is accessible

I have the following code: class Hello { class Thing { public int size; Thing() { size = 0; } } public static void main(String[] args) { Thing thing1 = new Thing(); …
coolpapa
  • 3,425
  • 2
  • 14
  • 8
265
votes
7 answers

Getting hold of the outer class object from the inner class object

I have the following code. I want to get hold of the outer class object using which I created the inner class object inner. How can I do it? public class OuterClass { public class InnerClass { private String name = "Peakit"; } …
peakit
  • 25,979
  • 25
  • 58
  • 77
230
votes
8 answers

Why Would I Ever Need to Use C# Nested Classes

I'm trying to understand about nested classes in C#. I understand that a nested class is a class that is defined within another class, what I don't get is why I would ever need to do this.
Dan Norton
203
votes
5 answers

Why would one use nested classes in C++?

Can someone please point me towards some nice resources for understanding and using nested classes? I have some material like Programming Principles and things like this IBM Knowledge Center - Nested Classes But I'm still having trouble…
bespectacled
  • 2,441
  • 4
  • 23
  • 30
187
votes
10 answers

Why can outer Java classes access inner class private members?

I observed that Outer classes can access inner classes private instance variables. How is this possible? Here is a sample code demonstrating the same: class ABC{ class XYZ{ private int x=10; } public static void main(String...…
Harish
  • 6,510
  • 10
  • 32
  • 45
145
votes
15 answers

Why can't we have static method in a (non-static) inner class?

Why can't we have static method in a non-static inner class? If I make the inner class static it works. Why?
Rahul Garg
  • 7,950
  • 8
  • 30
  • 28
127
votes
5 answers

Can inner classes access private variables?

class Outer { class Inner { public: Inner() {} void func() ; }; private: static const char* const MYCONST; int var; }; void Outer::Inner::func() { var = 1; } const char* const Outer::MYCONST =…
kal
  • 26,197
  • 48
  • 119
  • 147
127
votes
13 answers

How to access outer class from an inner class?

I have a situation like so... class Outer(object): def some_method(self): # do something class Inner(object): def __init__(self): self.Outer.some_method() # <-- this is the line in question How can I access…
T. Stone
  • 18,208
  • 15
  • 67
  • 95
126
votes
4 answers

How to instantiate non static inner class within a static method?

I have the following piece of code: public class MyClass { class Inner { int s, e, p; } public static void main(String args[]) { Inner in; } } Up to this part the code is fine, but I am not able to instantiate 'in' within…
Victor Mukherjee
  • 9,389
  • 15
  • 46
  • 83
124
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
  • 241,635
  • 91
  • 474
  • 626
1
2 3
99 100