The Artima Developer Community
Sponsored Link

Java Buzz Forum
Static class in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Static class in java Posted: Jul 11, 2015 1:04 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Static class in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Yes we can create a class as static. But class should be inner class or nested class.
  • We know how to create static methods, static variables and static blocks.
  • As Java Supports defining a class within a class we can create a static inner class inside a class.
  • This inner static class inside a class can access static members of outer class even its private.

Java Program to create static inner class:


  1. package instanceofjavaTutorial;
  2.  
  3. public class Outer{ 
  4.  
  5.   static class inner{
  6.  
  7. public void print(){
  8.  
  9. System.out.println("static inner class method called");
  10.  
  11. }
  12.  
  13. }
  14.  
  15. public static void main(String args[]){
  16.  
  17. Outer.inner in= new Outer.innner();
  18.  
  19. in.print();
  20.  
  21. }

Output:

  1. static inner class method called

Static inner class accessing outer class static variable:

  1. package instanceofjavaTutorial;
  2.  
  3. public class Outer{ 
  4.  static int a=10;
  5.   static class inner{
  6.  
  7. public void print(){
  8.  
  9. System.out.println(a);
  10.  
  11. }
  12.  
  13. }
  14.  
  15. public static void main(String args[]){
  16.  
  17. Outer.inner in= new Outer.innner();
  18.  
  19. in.print();
  20.  
  21. }

Output:

  1. 10

  • In java there are two types of nested classes one is static and another one is non static nested classes.
  • We saw static classes in java lets see what will be there in non static nested classes.
Non-static nested class(inner class)
  1. Member inner class
  2. Anonymous inner class
  3. Local inner class

  • A class is defined within a class and outside of methods of that class known as member inner class.
  • Anonymous inner class is a inner class which does not have a name and whose instance is created at the time creating class itself.
  • A class which is defined inside a method of another class known as local inner class

Click here for more information about inner classes

Read: Static class in java

Topic: The Evolution of Database Schemas using SQL + NoSQL Previous Topic   Next Topic Topic: Java : Collection Framework : Collections (BinarySearch Comparator)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use