Copyright

Inner Classes in Java: Definition & Example

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Java lets you create a class within another class. It's a good way to protect data and limit the scope of your program: This lesson will define the types of inner classes and provide working code examples.

Inner Classes

An inner class, also called a nested class, is a class within a class. Nesting classes is important for organizing classes that are specific to a specific task: Think of a nested class as more of a tool for the outer/parent class. You might have a LogFile cache that logs updates or changes. You could then create a LogFileDetail class that stores some more information about the log file. Not everybody needs to see this detail (they may only care about the item that was logged), but you can still show the detail if needed.

Nested classes support a key object-oriented concept of encapsulation, or data hiding. If we make the LogFileDetail class private, it's inaccessible to outsiders.

Finally, nested classes can make code a little easier to maintain. By having LogFile and LogFileDetail next to each other, the code may be easier to read and maintain. But don't go overboard! We need to strive to make our code concise and functional. Yes, you can nest classes. But don't nest them seven layers deep!

Let's take a look at how an outer/inner class looks in Java:

//outer class:
public class LogFile {
//inner class:
 public class LogFileDetail {
  public void hello() {
   System.out.println('Inner Class!');
  }
 }
}

Types of Inner Classes

There are two main categories of inner classes. We call these static and non-static. A nested static class cannot directly access variables in the outer class. It needs to refer to an instance of the outer class. A non-static nested class is really a plain inner class. Let's look at some examples of each type.

Non-Static Nested Class

The previous code sample showed how a non-static inner class works. Creating a new instance of the nested/inner class may look a little different than what you're used to. Let's take a look at an example using our LogFile (outer) and LogFileDetail (inner) classes:

public static void main(String[] args) {
 LogFile log = new LogFile();
 LogFile.LogFileDetail logDetail = log.new LogFileDetail;
}

You'll notice right away that use of the new keyword looks quite different; the new keyword is used AFTER the name of the outer class when we create the new instance of the inner class.

This non-static class can access variables of the outer class. Let's look at a variation of our two classes; this one has a variable in the outer class (LogFile), but we will be able to access it from our instance of the inner class.

First, the new code for LogFile and LogFileDetail is below. We've created a variable in the LogFile class that sets a counter. In the LogFileDetail class, we display that value:

//outer class:
public class LogFile {
//outer class variable:
 int counter = 17;
 //inner class
 public class LogFileDetail {
  public void hell() {
   System.out.println('Detail: ' + counter);
  }
 }
}

In our main method, we can access the hello() method from the instance of the inner class we created.

public class MyMainClass {
 public static void main(String[] args) {
  LogFile log = new LogFile();
  LogFile.LogFileDetail logDetail = log.new LogFileDetail();
  //access the hello method
  logDetail.hello();
 }
}

There is another inner class that is part of the non-static family, and that is the local class.

Local Classes

A local class is actually defined WITHIN a method (as opposed to the class). It's not a class within a class, but a class within a method. A local class can only be accessed inside the method. They can still get to variables and methods of the class (just like other inner classes).

public class Log File {
 void localMethod() {
  long logID = 2999993984;
  //local class
  class LocalClass {
   public void display() {
    System.out.println('Inside the local class: ' + logID);
   }
  }
  //create new instance of local class
  LocalClass local = new LocalClass();
  local.display();
 }
}

Static Nested Class

In the following example, the only way to get at the function hello() is to create an instance of the outer class (LogFile). A static inner class can't directly reference variables or methods in the outer class. The only way to get through them is to create a reference to the outer class.

In Java, the code for this action is displayed below:

public class LogFile {
 public static class LogFileDetail {
  public void hello() {
   System.out.println('Inner Class!');
  }
 }
}
public static void main(String[] args) {
 LogFile.LogFileDetail detail = new LogFile.LogFileDetail();
 detail.hello();
}

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support