Static class in Java
Can a class be static in Java?
The answer is YES, we can have static class in java. In java, we have static instance variables as well as static methods and also static block. Classes can also be made static in Java.Java allows us to define a class within another class. Such a class is called a nested class. The class which enclosed nested class is known as Outer class. In java, we can’t make Top level class static. Only nested classes can be static.
Nested Classes
The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here:class OuterClass {
...
class NestedClass {
...
}
}
Nested classes are divided into two categories: static and non-static. Nested classes that are declared
Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
here is the code sample of using both nested static class vs non static class :
What are the differences between static and non-static nested classes?
static
are called static nested classes. Non-static nested classes are called inner classes.
1) Nested static class doesn't need reference of Outer class but non
static nested class or Inner class requires Outer class reference. You can not
create instance of Inner class without creating instance of Outer class. This
is by far most important thing to consider while making a nested class static
or non static.
2) static class is actually static
member of class and can be used in static
context e.g. static method or static block of Outer class.
3) Another difference between static and non static nested class is that
you can not access non static members e.g. method and field into nested static
class directly. If you do you will get error like "non
static member can not be used in static context". While Inner class
can access both static and non static member of Outer class.
Static Nested Classes
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
Inner Classes
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.here is the code sample of using both nested static class vs non static class :
/*
* Java program to demonstrate What is nested static and non static class.
* How to create instance of static and non static class and How to call
* methods of nested static and Inner class in Java. Overall comparison of
* static vs non static class.
*/
class Outer{
private static String message = "HelloWorld";
// Static nested class
private static class MessagePrinter{
//Only static member of Outer class is directly accessible
//in nested static class
public void printMessage(){
// Compile time error if message field is not static
System.out.println("Message from nested static class : " + message);
}
}
//non static nested class - also called Inner class
private class Inner{
// Both static and non static member of Outer class
//
is accessible in this Inner class
public void display(){
System.out.println(" Message from non static nested or Inner class : " + message);
}
}
// How to create instance of static and non static nested class
public static void main(String... args){
// creating instance of nested Static class
Outer.MessagePrinter printer = new Outer.MessagePrinter();
//calling non static method of nested static class
printer.printMessage();
// creating instance of non static nested class or Inner class
// In order to create instance of Inner class you need an
//Outer class instance
Outer outer = new Outer();
//outer class instance for creating non static nested class
Outer.Inner inner = outer.new Inner();
//calling non static method of Inner class
inner.display();
// we can also combine above steps in one step to
//
create instance of Inner class
Outer.Inner nonStaticIner = new Outer().new Inner();
// similarly you can now call Inner class method
nonStaticIner.display();
}
}
Output:
Message from nested static class : HelloWorld
Message from non static nested or Inner class : HelloWorld
Message from non static nested or Inner class : HelloWorld
Why Use Nested Classes?
Compelling reasons for using nested classes include the following:- It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
- It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared
private
. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world. - It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.
0 comments:
Post a Comment