Codementor Events

How I learned Java nested class

Published Jan 14, 2021

About me

I'm a software engineer, currently working with Java.

Why I wanted to learn Java nested class

I needed to learn this to get Java readability in my company. I haven't encountered Java nested class so far though.

I also wanted to share what I'm capable of with other people. I feel super good when helping people. Hopefully, you will find that helpful. These blog post are non-profitable.

How I approached learning Java nested class

Find a good source of truth.

I found that The Java Tutorials series provided by Oracle is super reliable.
I did a quick Google search and this is what I found:
https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Download IntelliJ IDEA

We need an IDE (Integrated Development Environment). This is a program, where we can write code, and it helps us doing many non-coding tasks such as building a program, debuging a program, downloading libraries, etc.

You can download the IDE at https://www.jetbrains.com/idea/download

Read the tutorials

I know theory is often boring, but trust me, that's something you cannot ignore forever. Key takeaways from the reading:

  • A nested class is simply a class within another class
  • Why nested classes?
    • Use case 1: implement a tree. For example, if MyTree is an outer class, then MyNode is an inner class. The key point is that MyNode doesn't have to be used by other classes rather than MyNode.
    • Use case 2 (quite rare): A and B are 2 top-level classes, where B needs access to members of A. However, A wants its members to be private. B can then become a non-static nested class of A in order to access A's members.

alt

Write code

I created a simply java file and play with nested classes. I'm implementing a tree.

Write a blog post about it

Then here comes the blog post 😃 I've found that sharing what you have learned is always a great way to learn it.

Challenges I faced

Laziness. I didn't have that much motivation to learn this part of Java since I haven't done anything related to Java nested class in the past 2 years at Google and I have never ever taken any Java course or learned Java in a proper manner.

Key takeaways

Sharing what you learn is a great way to learn it

Tips and advice

Write a blog post about what you learn.

Final thoughts and next steps

I'll try to write more about what I learn in the coming blog posts.

Discover and read more posts from Cuong Nguyen
get started
post commentsBe the first to share your opinion
Show more replies