Copyright

Static Nested Classes in Java: Definition & Example

Instructor: Benjamin Blanchard

Ben has taught ESL and web programming and has a M.S. in education.

A static nested class in Java is a class that is defined within another class but retains most of the characteristics of an independent class. Learn why this can be useful in the following lesson.

A nested class in Java is basically a class that is defined within another class, so it's a member, just like variables and methods. To see what makes a static nested class different from a non-static one, let's review what makes a static member different from a non-static member.

Defining Static

When you define a member of a class as static you are saying that it should not be associated with any particular object that will be instantiated from that class. For this reason, they are sometimes referred to as class members, that is, members of the class as a whole, rather than attributes or behaviors of individual objects. You can think of them as resources that are available wherever the class is available.

In the case of static nested classes, this essentially means that you get an extra class in the same package. This extra class interacts with the containing class in the same way any other class would, it just happens to be neatly packaged up with that containing class, so it comes along with it. Let's look at a simple example:

Basic static nested class example

If you compile and run this code, it should output the string 'from the nested class'.

All you need to do to create a static nested class is to create a class within another class and add the word 'static' at the beginning of the definition.

As you can see, you don't need to instantiate the containing class in order to create an object from the static nested class. You access it by name using a dot (.) the same as you would any other member. It's like you have another class available that just happens to have a dot in its name (which is usually illegal). In this case, we've created an object called 'object' of type OuterClass.Nested.

Uses for Static Nested Classes

A common use case for static inner classes is the situation where we only need a certain class (call it 'class B') when we're using a certain other class (call it 'class A'). If we make class B a static nested class of class A, we can be sure to only load class B when we load class A. This is efficient. They act independently but always show up together.

Ok, so let's look at a more useful example:

Java static nested class code example for Employee class

Here we have a class that represents an employee. One of the things we want to be able to do is calculate the amount paid to a given employee for a pay period. We'd also like to be able to calculate overtime pay. It might be nice to have this capability even when we're not dealing with a particular employee. So our application can report general information about overtime pay. We'll make an 'OvertimeCalculator' class for this. Two reasons this is a good candidate for a static nested class are:

  • We probably won't need it in any other context.
  • We'll likely need it frequently when we're dealing with employee data.

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