Solved

nested class vs inner class

Posted on 2016-08-16
5
46 Views
Last Modified: 2016-08-20
Hi,

What are differenced between nested class vs inner class. when and how and where we use each one. please advise
0
Comment
Question by:gudii9
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 41758794
There's no difference; the two terms mean the same thing.
0
 
LVL 26

Accepted Solution

by:
dpearson earned 250 total points
ID: 41758828
To me "nested class" can mean either "static class" or "inner class".

So like this:
public class Outer {
   public static class StaticNested { }    // Note: "static" keyword here.  This is a static nested class.
}

public class Outer {
   public class Inner { }     // Can also be considered a non-static nested class
}

Open in new window


You asked a similar question a little while ago gudii9 about when to use these two and my advice was "always use the static version" - because they behave like normal classes.

Inner classes have special and confusing behaviors.  In my opinion they're a mistake in the Java language design and best avoided.

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 41759476
To me "nested class" can mean either "static class" or "inner class".

so inner class means non static one right which is one kind of nested class right ? ( while static lass is other kind right?)
0
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 250 total points
ID: 41759499
Looks like Doug is right. Per the language specification:
An inner class is a nested class that is not explicitly or implicitly declared static.
If you do GUI work, you'll become pretty familiar with inner (non-static) classes. If you use lambda expressions, you're secretly using inner classes, too. The key with them being non-static is that they can access stuff in the outer class, while static nested classes cannot.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41763713
To me "nested class" can mean either "static class" or "inner class".

So like this:
public class Outer {
   public static class StaticNested { }    // Note: "static" keyword here.  This is a static nested class.
}

public class Outer {
   public class Inner { }     // Can also be considered a non-static nested class
}


above is clear
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now