What is Class in Java?
A Class in Java is nothing but a blueprint or template for an Object. The
System or say the java virtual machine uses this blueprint for creating Object
of that class type.
Everything is sort of Class in Java, it is created with a access modifier like
public followed by the keyword class and then the name of the Class and then
curly braces : "public class KodeSrc {} ".
Need to understand Classes in Java in depth? See this post -- Demystifying Classes and Objects in Java : Everything You Need to Know
| By KodeSrc
What are nested Classes In Java?
When we declare a class inside another class in Java , these classes are
called as nested classes.If a nested class is declared as static then it
is called as static class and if it is non static it is called as inner class
in Java.
What are Inner Classes in Java?
Inner Classes in Java are those nested non static classes that are declared
inside another class. An Inner class can access all members of the outer class
of any access modifiers even the private members. Yup the inner classes gets a
privileged access of the outer class members.
How to Create Inner Classes in Java?
Its really simple to create inner classes , just declare the class inside an
outer class of which you want an inner class. Basically if you want to create
an inner class B inside the Class A. Then the class definition of B will be
inside class A.
Let's understand it with couple of examples.
Say we have a class KodeSrc with following definition
class KodeSrc{
private String name;
public String getName(){
return this.name;
}
public void setName(String name){
this.name=name;
}
}
Now if we want to access the private variable name, can we access it
directly using "." operator from another Main Class?
Answer is simple, No we cannot access it out the class as its private
so its scoped within that class.
public class Main{
public static void main(String args[])
{
KodeSrc kodeSrc=new
KodeSrc();
kodeSrc.name="KodeSrc!";
}
}
Output:
/Main.java:14: error: name has private access in KodeSrc
kodeSrc.name="KodeSrc!";
^
1 error
This is about the access but Now lets create an Inner Class for this case.
class KodeSrc{
private String name;
public String getName(){
return this.name;
}
public void setName(String name){
this.name=name;
}
class KodeSrcInnerClass{
private String innerClassName;
public String getInnerClassName(){
return this.innerClassName;
}
public void setInnerClassName(
String innerClassName)
{
this.innerClassName=innerClassName;
}
}
}
Ok So when we say that inner class can access members of the outer classes
that means that an object of inner class can access an object of outer class.
So will the inner class access any instance of the outer class? No . The Inner
class should be bound to a specific object of the outer class.
How to create instance of inner class in java?
We cannot create instance of inner class without creating the instance of
outer class.
You can do it in 2 ways
1st Way: Create a reference variable inside the outer class and let
the method of the outer class create an instance of the inner class. Wait Wait
don't get confused I'll make it clear for you using an example.
Step 1: Declare the variable of inner class
private KodeSrcInnerClass kodeSrcInnerClass;
public KodeSrcInnerClass getKodeSrcInnerClass() {
return this.kodeSrcInnerClass;
}
public void setKodeSrcInnerClass() {
this.kodeSrcInnerClass = new KodeSrcInnerClass();
}
Step 3: Now you can access inner class object from in this way
KodeSrc obj=new KodeSrc();
obj.setKodeSrcInnerClass();
obj.getKodeSrcInnerClass().setInnerClassName("Inner Class Of KodeSrc");
public class KodeSrc{
private String name;
public KodeSrcInnerClass getKodeSrcInnerClass() {
return this.kodeSrcInnerClass;
}
public void setKodeSrcInnerClass() {
this.kodeSrcInnerClass = new KodeSrcInnerClass();
}
private KodeSrcInnerClass kodeSrcInnerClass;
public String getName(){
return this.name;
}
public void setName(String name){
this.name=name;
}
public class KodeSrcInnerClass{
private String innerClassName;
public String getInnerClassName(){
return this.innerClassName;
}
public void setInnerClassName(
String innerClassName)
{
this.innerClassName=innerClassName;
}
}
}
public class Main {
public static void main(String[] args) {
KodeSrc obj=new KodeSrc();
obj.setKodeSrcInnerClass();
obj.getKodeSrcInnerClass()
.setInnerClassName("Inner Class Of KodeSrc");
System.out.println(
obj.getKodeSrcInnerClass()
.getInnerClassName()
);
}
}
2nd Way: Creating it directly from the main method.
public class Main {
public static void main(String[] args) {
KodeSrc obj=new KodeSrc();
KodeSrc.KodeSrcInnerClass kodeSrcInnerClassObj=
obj.new KodeSrcInnerClass();
kodeSrcInnerClassObj
.setInnerClassName("Inner Class Of KodeSrc Instantiated in Main");
System.out.println(
kodeSrcInnerClassObj
.getInnerClassName()
);
}
}
What are Static Classes in Java?
The Static class are declared with static keyword during the class
declaration. We cannot declare a top level class as static, compiler will
throw error for this.
static class KodeSrcStaticClass{
private String name;
}
This will not be allowed by compiler.
We can create nested classes as static, i.e a nested class when made
static is called static class whereas if it is not static then it's called
as Inner Class.
public class KodeSrc{
private String name;
public KodeSrcInnerClass getKodeSrcInnerClass() {
return this.kodeSrcInnerClass;
}
public void setKodeSrcInnerClass() {
this.kodeSrcInnerClass = new KodeSrcInnerClass();
}
private KodeSrcInnerClass kodeSrcInnerClass;
public String getName(){
return this.name;
}
public void setName(String name){
this.name=name;
}
public static class KodeSrcInnerClass{
private String innerClassName;
public String getInnerClassName(){
return this.innerClassName;
}
public void setInnerClassName(
String innerClassName)
{
this.innerClassName=innerClassName;
}
}
}
Why we use static class as nested class?
- The main use of this is that we don't need to create object of the outer class for accessing the inner class. We can directly use the name of the outer class and access the inner class.
- It also helps in controlling access to non static members of outer class.
- If we declare any method, variables as static it is bound to the class and not to instance of that class, for example if we create a class with a variable counter as static then each object will have same copy of the variable , any change to that variable will be reflected across all objects.
- One of the use case "Singleton class."
public class Main {
public static void main(String[] args) {
KodeSrc.KodeSrcInnerClass kodeSrcInnerClassObj=
new KodeSrc.KodeSrcInnerClass();
kodeSrcInnerClassObj
.setInnerClassName("Inner Class Of KodeSrc Instantiated in Main");
System.out.println(
kodeSrcInnerClassObj
.getInnerClassName()
);
}
}
Points to remember for Static and Inner Classes In Java :
- For creating object of inner classes, object of outer class in required.
- In case of static classes,, to create object of static class outer class object is not required.
- Static classes can only access static members of outer class.
- Inner class can access both static and non static members of outer class.
Conclusion
In this post we have understood what is class, what are nested classes,
What are Static and Inner classes and how to implement it.
Interested in understanding few more topics in Java?
Check it Out !
Please Let me Know, If you have any doubts.
Please Let me Know, If you have any doubts.