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
2059
votes
28 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
  • 27,619
  • 12
  • 30
  • 34
411
votes
12 answers

Is not an enclosing class Java

I'm trying to make a Tetris game and I'm getting the compiler error when I try to create an object Shape is not an enclosing class public class Test { public static void main(String[] args) { Shape s = new Shapes.ZShape(); } } I'm…
V Sebi
  • 4,253
  • 2
  • 15
  • 8
386
votes
15 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
  • 10,900
  • 15
  • 70
  • 100
382
votes
8 answers

Java: Static vs inner class

What is the difference between static and non-static nested class?
Abhishek Sanghvi
  • 4,582
  • 6
  • 28
  • 36
358
votes
11 answers

What causes error "No enclosing instance of type Foo is accessible" and how do I fix it?

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,695
  • 2
  • 16
  • 8
348
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
  • 13,831
  • 10
  • 55
  • 70
282
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
  • 28,597
  • 27
  • 63
  • 80
249
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
237
votes
6 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,801
  • 4
  • 25
  • 35
201
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
  • 7,589
  • 10
  • 36
  • 47
175
votes
16 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
  • 19,209
  • 15
  • 69
  • 97
153
votes
15 answers

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

Why can't we have static method in a non-static inner class? public class Foo { class Bar { static void method() {} // Compiler error } } If I make the inner class static it works. Why? public class Foo { static class Bar { //…
Rahul Garg
  • 8,410
  • 8
  • 33
  • 28
151
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
  • 28,545
  • 49
  • 129
  • 149
142
votes
11 answers

Nested or Inner Class in PHP

I'm building a User Class for my new website, however this time I was thinking to build it little bit differently... C++, Java and even Ruby (and probably other programming languages) are allowing the use of nested/inner classes inside the main…
Lior Elrom
  • 19,660
  • 16
  • 80
  • 92
136
votes
5 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
  • 10,487
  • 16
  • 54
  • 97
1
2 3
99 100