Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Help Java N Java Nested Class

Related Tutorials


 
 

Share on Google+Share on Google+

Java Nested Class

Advertisement
In Java programming language, when a class is defined within another class then such a class is called a nested class.

Java Nested Class  

     

In Java programming language, when a class is defined within another class then such a class is called a nested class. Nested classes are a feature of Java that is included in jdk1.1.

The reasons of why we use nested classes are:

  • It is a way of logically grouping classes that are only used in one place.
  • It increases encapsulation.
  • Nested classes can lead to more readable and maintainable code.

These classes are divided into two categories: static and non-static.

Nested classes that are declared static are simply called static nested classes. A static class has no access to instance-specific data.

Non-static nested classes are called inner classes. It has access to all of its enclosing class's instance data, including private fields and methods.

Nested classes are associated with the enclosing class itself, whereas inner classes are associated with an object of the enclosing class.

The given class structure shows the way of using the nested class.

 

class OuterClass {

  ...

  static class StaticNestedClass {

  ...

  }

  class InnerClass {

  ...

  }

}

 

 

 Java main Method

 

Every Java program must have one main method. The main method is the first method, which the Java Virtual Machine executes. In other words, when you execute a class with the Java interpreter, the runtime system starts by calling the class's main() method. The main() method then calls all the other methods required to run your application. The main method is the entry point in the Java program and java program can't run without main method.

The signature of main() method looks like this:

public static void main(String args[])

 

The method signature for the main() method contains three modifiers:

  • public indicates that the main() method can be called by any object.
  • static indicates that the main() method is a class method.
  • void indicates that the main() method has no return value.

Read more at

http:/www.roseindia.net/java/master-java/underStandingHello.shtml

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 27, 2009

Related Tutorials

Discuss: Java Nested Class  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com