This post originated from an RSS feed registered with Java Buzz
by Ben Hosking.
Original Post: Nested Classes and how they work
Feed Title: A Funny Java Flavoured Look at the World
Feed URL: http://businesslogs.com/WebLog/RSS.xml
Feed Description: The blog looks at using Java and programming in general as it pops up in life as a Java programmer. It will have links to interesting Java articles and resources. It will also have a lot of SCJP Java 1.5 information and links as I am currently studying
One of the most confusing parts of Java our nested classes. The syntax can be very tricky with classes inside methods and methods overwriting others and semi colons on the end of curly brackets, the bottom line is it can be be very confusing.
Nested classes are probably one of the biggest SCJP Java 5 exam gotchas out there. The Java exam has a will not compile option on every question and inner classes makes this option very tricky because there syntax can look wrong and make the code look like it won't compile.
I really like the two sample chapters I have read from Hardcore Java here is a quote about the book from the OReilly website
"Hardcore Java takes this language and breaks it apart, piece by piece, revealing the important secrets and tricks that will take you from a junior-level programmer to a seasoned and expert developer."
nested classes in Java is one of the more confusing concepts and can make reading someone else's code very confusing, especially if you are not to sure how nested classes work. The chapter also squashes the myth/misinterpretation that Static nested classes are inner classes. With most things in Java the best way to understand the programming concepts being talked about is to give examples and this sample chapter gives lots of examples.
if you don't understand inner classes I would recommend reading these articles because they can be very useful. The way I look at Inner classes is that they are like composition and using another class in your code, except the inner class has access to all of your private variables, which is a big advantage. If you are studying for the SCJP exam I definitely recommed you read these articles because you will see a lot of inner class code in the exam and you must be able to tell what is legal and illegal code otherwise you could be throwing away a lot of marks by not being able to spot the difference.
The articles talk about these topics and each article looks at each topic in some depth