aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner classes(SCJP 1.5) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner classes(SCJP 1.5)" Watch "Inner classes(SCJP 1.5)" New topic
Author

Inner classes(SCJP 1.5)

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Hi
Can any one help me understanding inner v
classes and there rules as every when I read tjhe K&B book I understood that but when I try to solve questions on that got confused. what should come and what should not come.
Please suggest me some simple way to understand the rules and stucture of inner classes.

Thanks in Advance


SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Divya,

Where do you miss the concept?

1- Inner classes, you instantiate an inner class you must have reference of
the outer class that is called enclosing class.

2- You don't need any outer class reference to instantiate the static inner
classes.

There are the two important things, regarding inner classes and static inner
classes.

Do you find confusion regarding them or something method inner or
anonymous class causes confusion?


Thanks,


cmbhatt
Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Hi,
I really getting confused while solving mock exams for iner classes
regarding Inner classes adn anonymous also.
Could you please elaborate on this.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,

Anonymous class



Line #1:
Alian a1 = new Alien() { <-- see this curly bracket

Here a1 is the ref variable of the anonymous class (a class with no name),
that is subclass of Alien.
In other words, the anonymous class is subclass of the Alien class and a1
is a reference variable of the anonymous class.

Using ref variable of the anonymous class, you can only access the methods
of its parent class (Alien in our case). You can't access anonymous class
specific method using that.

Note: An anonymous class either extends some class or implements some interface. But not both together. There is nothing like extends or
implements keyword you use for this work.

Line #2: You override the parent class (Alien class) method wonder in this.


Line #3: Always see the semicolon, after the anonymous class definition
completes.


Thanks,
Aaron Raja
Ranch Hand

Joined: Jun 07, 2007
Posts: 206
just keep these also in mind
-----------------------
Here is the rule:
If inner class is declared in a method then it can access only final variables of the particular method but can access all variables of the enclosing class.
look the code below carefully!!!

System.out.println(z);
will give you error because z is declared in Innermethod01 as int z = 1; not final!
wow!
so inorder to compile it make it final and declare in Innermethod01 as final int z =1.
-----------------------


code:
--------------------------------------------------------------------------------

public class Class01 {
int a = 0;
int b = 222;

public class Inner {}

public void Innermethod01() {
final int x = 1;
final int y = 1;
int z = 1;

class InnerClass01 {
int c = 20;
void Innermethod02() {
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
}
}
[ June 27, 2007: Message edited by: Aaron Raja ]

Thanks, AR
SCWCD 5, SCJP 1.4, OCA (PL/SQL)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner classes(SCJP 1.5)