Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

more about nested class topics

 
William Yan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read Corey's SCJP tiplines on inner class topics, there are 4 types of inner class: inner, nested, anonymous, local. But the terms in Sun Certified Java Programmer Pre-Exam Essentials seem a little different: non-static nested, static nested, amonymous, non-static nested in method's scope. I am not very sure of the terms and concepts that i catch, so i write the code below for REVIEWs if i have made mistakes. Is this enough for the exam? THANK YOU.
Yours,
William.

[ October 17, 2006: Message edited by: William Yan ]
 
Maneessh saxena
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi William,

Below is the explanation . I hope you will understand clearly after reading this . if you still have any confusion please let me know..


Inner or non-static nested : :

the term Inner here to represent inner classes that are not :

Static
Method-local
Anonymous

Example::

class MyOuter {
private int x = 7;
// inner class definition i.e not static
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
}
} // close inner class definition
} // close outer class


Nested or static nested ::

Static nested classes referred to as static inner classes, but they really aren't inner classes at all, by the standard definition of an inner class.
While an inner class (regardless of the flavor) enjoys that special relationship with the outer class (or rather the instances of the two classes share a relationship), a static nested class does not.

Actually there is no static class at all.it’s like a static member of a class in which it’s declared.
Example :

class BigOuter {
static class Nested { }
}



Amonymous Class ::


inner classes declared without
any class name at all (hence the word anonymous).

you can define these classes not just within a method, but even within an argument to a method.

Example ::

class Popcorn {
public void pop() {
System.out.println("popcorn");
}
}
class Food {
Popcorn p = new Popcorn() {
public void pop() {
System.out.println("anonymous popcorn");
}
};
}


Laocal or non-static nested in method's scope ::

Any class i.e declared within the scope of a method .even though we can have these classes in loops any block etc.

Example ::

class MyOuter2 {
private String x = "Outer2";
void doStuff() {
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
} // close inner class method
} // close inner class definition
} // close outer class method doStuff()
} // close outer class



Regards…..
 
William Yan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, raunak.
The concept seems the same in your code and mine.

Regards,
William
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic