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
36
votes
2 answers

Accessing outer class variable in inner class

In inner classes, variables of outer class are accessible, but local variables of a method are not. I understood the part about local variables of a method not being accessible but I want to know why outer class variables are accessible? My…
kiran
  • 445
  • 1
  • 5
  • 11
35
votes
5 answers

Is there a way to get a list of innerclasses in C#?

As per the title. I'd like a list of all the inner classes of a given class, it can be a list of names or a list of types - I am not fussed. Is this possible? I thought there might be a way using reflection but haven't been able to find one.
Dr. Thomas C. King
  • 993
  • 2
  • 15
  • 28
34
votes
2 answers

Why are static methods allowed inside a non-static inner class in Java 16?

We know that a non-static inner class can be accessed using the instance of the outer class, so a static method is less meaningful inside a non-static class. But from Java 16 static methods are allowed inside a non-static inner class. Why did this…
Pradeesh tet
  • 617
  • 7
  • 16
34
votes
7 answers

Is it possible to create an instance of inner class using Java Reflection?

Sample of code: public class Foo { public class Bar { public void printMesg(String body) { System.out.println(body); } } public static void main(String[] args) { // Creating new…
kars7e
  • 786
  • 1
  • 6
  • 19
34
votes
5 answers

"Non-static variable this cannot be referenced from a static context" when creating an object

I wrote the below code to test the concept of classes and objects in Java. public class ShowBike { private class Bicycle { public int gear = 0; public Bicycle(int v) { gear = v; } } public static void…
mko
  • 21,334
  • 49
  • 130
  • 191
33
votes
4 answers

Why do inner classes make private methods accessible?

I don't understand why this compiles. f() and g() are visible from the inner classes, despite being private. Are they treated special specially because they are inner classes? If A and B are not static classes, it's still the same. class NotPrivate…
mparaz
  • 2,049
  • 3
  • 28
  • 47
33
votes
5 answers

Why is an anonymous inner class containing nothing generated from this code?

package com.test; public class OuterClass { public class InnerClass { public class InnerInnerClass { } } public class InnerClass2 { } //this class should not exist in OuterClass after dummifying private…
32
votes
6 answers

Why can't inner classes declare static members?

The Java Tutorial says that since an inner class is associated with an instance of the enclosing class, it (the inner class) cannot define any static members itself. It's interesting for me why can't inner classes declare static members for…
32
votes
5 answers

How do I Use Inner Classes in PHP?

I'm from a Java background, and I want to use an inner class in php. Every time I put the inner class though, I get a syntax error. Is this possible with PHP? Also, how do I reference the outer class? Do I get access to ALL its data…
Lindz
  • 321
  • 1
  • 3
  • 4
32
votes
10 answers

Do java's Inner classes pose a security risk?

Recently the security team on my project released a secure code guidelines document, designed to be used as part of our code reviews. The first thing that struck me was an item that said "Do not use Inner classes". I thought this seemed like a very…
mR_fr0g
  • 8,462
  • 7
  • 39
  • 54
31
votes
1 answer

Access outer class "super" from inner class in Java

How can I access outer class' super from an inner class? I'm overriding a method to make it run on a different thread. From an inline Thread, I need to call the original method but of course just calling method() would turn into an infinite…
pistacchio
  • 56,889
  • 107
  • 278
  • 420
30
votes
1 answer

Memory leakage in event listener

I have gone through the article http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html. In this article it is suggested to use a static inner class with a WeakReference . Many inner classes are used for event listeners. Can those…
dev_android
  • 8,698
  • 22
  • 91
  • 148
29
votes
5 answers

How to reference nested classes with css?

When using CSS, how can I specify a nested class? Here is my HTML markup:

Collapsable

Simon
  • 7,991
  • 21
  • 83
  • 163
28
votes
9 answers

Using Inner classes in C#

What are the best practices regarding the use and structure of inner classes in C#. For instance if I have a very large base class and two large inner classes should I split them up into separate (partial class) codefiles or leave them as one very…
DevinB
  • 8,231
  • 9
  • 44
  • 54
28
votes
3 answers

Why is a serializable inner class not serializable?

The following code: public class TestInnerClass { public static void main(String[] args) throws IOException { new TestInnerClass().serializeInnerClass(); } private void serializeInnerClass() throws IOException { File…
tb189
  • 1,942
  • 3
  • 22
  • 37