Except for GUIs, could anyone give me an example of why we need inner classes?
Hey zeroliken, long time no see! Remember the thread "Programming for..."? Well, this doubt is with regard to that thread. I am teaching my son Java. Now he can't understand the point of inner classes. Could you, kind of explain it in an easy manner?
Ask me if you want the book's pages which explain inner classes.
Yes long time no see :)
well an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields
OK, so the inner classes gets access to private instance variables and methods of the outer class and vice - versa?
Can I scan the pages which he does not understand and show them to you so that you can explain?
I hope you can download!
I hope you can download!
the best explanation and reason for using inner classes/static inner classes is in the docs zeroliken gave you:
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)
Why Use Nested Classes?
There are several compelling reasons for using nested classes, among them:
It is a way of logically grouping classes that are only used in one place.
It increases encapsulation.
Nested classes can lead to more readable and maintainable code.
Logical grouping of classes—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.
Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...
Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.
This is the result of previous question answered :
code for the listbox - datagridview interaction
At the top of the code ...
I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...