Solved

nested class vs inner class

Posted on 2016-08-16
5
57 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 77
Error with Java/Cache JDBC Classpath 2 30
VB Script to add site to Java Exception List 4 49
tomcat not starting 6 29
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

911 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

23 Experts available now in Live!

Get 1:1 Help Now