What's the difference between declaring a class inside of another class and declaring a class in a separate file?.
Asked
Active
Viewed 64 times
0
-
1Are you asking about inner classes? – PM 77-1 May 29 '16 at 01:20
-
1The page in the Java Tutorials about [Nested Classes](https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) may help you. My advice is to do a Google search, and then come back with any questions you have afterwards. – 4castle May 29 '16 at 01:22
-
I think so. Is there a difference between classes that stand alone in the package and classes inside classes. – begginer3 May 29 '16 at 01:26
-
Please clarify your question with the source code of an example of both by what you mean. – Dmytro May 29 '16 at 01:28
-
I see. So nested classes are kind of like functions in C .You use them mostly so you dont have to repeat stuff and thus they make your program easier to read.Also you are only using/calling them inside the "super" class. Am I right or have a completely missed the point ? – begginer3 May 29 '16 at 01:31
-
It depends. If the nested class is declared `public static` then it behaves exactly as if it was not nested (though people will assume the classes are closely related). If the class is not `static` and is instead an inner class, then it could have many uses. Nested classes have the option of being `private`, which makes them very different. This question is a bit too broad for me to answer fully. – 4castle May 29 '16 at 01:35
-
Could you clarify your question? There are several questions already out there which discuss this, such as [Java inner class and static nested class](http://stackoverflow.com/q/70324/5743988) or [Static nested class in Java, why?](http://stackoverflow.com/q/253492/5743988) – 4castle May 29 '16 at 01:52
-
I have never made a nested class in java before. But when i am seeing tutorials ,the person in the video will (most of the time) not create new class files ,but he will create diferent classes inside one class in order to explain his point and so it can all be seen simultaneously on the video. And while I was looking at this ,I thought "hey,thats something that I never do, am I wrong ? ,are there any benefits ?" So thats why I asked this question in the first place ,and why it is so broad.Sorry for the inconvenience – begginer3 May 29 '16 at 01:59
1 Answers
0
Literally, nested classes were added to Java for two reasons: 1. Source code clarity. 2. Name-conflict reduction.
Java actually didn't need to support doing this, but they were totally doing programmers a "solid" because I'm sure they know how messy code can get when you're in the moment of it all.
To answer your question, explicitly: The difference is that there really IS NOT a difference, it just makes code easier to read and you end up with less name-conflict mistakes.

mboyde
- 69
- 6
-
Just to clarify, this answer is discussing `static` nested classes. It's not true in the general case. – 4castle May 29 '16 at 01:38
-
I wasn't entirely sure what the question was asking in specific, so I went with what I THOUGHT he/she must have been asking about. I guess I was thinking about about just static nesting when I answered originally. – mboyde May 29 '16 at 01:40
-