Demonstrates Static nested Classes : static Member « Class Definition « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Class Definition » static Member 
5.12.9.Demonstrates Static nested ClassesPrevious/Next
/*
 *     file: StaticNestedClassDemo.java
 *  package: oreilly.hcj.nested
 *
 * This software is granted under the terms of the Common Public License,
 * CPL, which may be found at the following URL:
 * http://www-124.ibm.com/developerworks/oss/CPLv1.0.htm
 *
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.
 * All Rights are Reserved by the various authors.
 *
 ########## DO NOT EDIT ABOVE THIS LINE ########## */

/**
 * Demonstrates Static nested Classes.
 
 @author <a href=mailto:[email protected]>Robert Simmons jr. (kraythe)</a>
 @version $Revision: 1.3 $
 */
public class StaticNestedClassDemo {
  /**
   * Main method.
   
   @param args
   *          Command line arguments.
   */
  public static void main(final String[] args) {
    OuterClass.SomeClass obj = new OuterClass.SomeClass();
    obj.someMethod();
    // OuterClass.SomeOtherClass prot = new OuterClass.SomeOtherClass(); // <=
    // Compiler error
    OuterClass.doPrivate();
  }
}

/* ########## End of File ########## */
/*
 * file: OuterClass.java package: oreilly.hcj.nested
 
 * This software is granted under the terms of the Common Public License, CPL,
 * which may be found at the following URL:
 * http://www-124.ibm.com/developerworks/oss/CPLv1.0.htm
 
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags. All
 * Rights are Reserved by the various authors.
 
 * ########## DO NOT EDIT ABOVE THIS LINE ##########
 */

/**
 * Demonstration of some static nested classes.
 
 @author <a href=mailto:[email protected]>Robert Simmons jr. (kraythe)</a>
 @version $Revision: 1.5 $
 */
class OuterClass {
  /** Holds the name. */
  private static final String name = "Robert";

  /** Holds the company. */
  private static String company = "O'Reilly";

  /** Holds a value. */
  private int value = 5;

  /**
   * Getter for the property company.
   
   @return The current company.
   */
  public static String getCompany() {
    return company;
  }

  /**
   * Getter for the variable name.
   
   @return The current name.
   */
  public static String getName() {
    return name;
  }

  /**
   * Getter for the variable company.
   
   @return the current company.
   */
  public int getValue() {
    return value;
  }

  /**
   * Demo of instatiation of a private class.
   */
  public static void doPrivate() {
    SomeOtherClass cl = new SomeOtherClass();
    cl.someMethod();
  }

  /**
   * A demo nested static class.
   
   @author <a href=mailto:[email protected]>Robert Simmons jr. (kraythe)</a>
   @version $Revision: 1.5 $
   */
  public static class SomeClass {
    /**
     * a demo method.
     */
    public void someMethod() {
      System.out.println(company);
      System.out.println(name);
      // System.out.println(value); // <= Compiler error
    }
  }

  /**
   * A demo nested static class.
   
   @author <a href=mailto:[email protected]>Robert Simmons jr. (kraythe)</a>
   @version $Revision: 1.5 $
   */
  protected static class YetAnotherClass {
  }

  /**
   * A demo nested static class.
   
   @author <a href=mailto:[email protected]>Robert Simmons jr. (kraythe)</a>
   @version $Revision: 1.5 $
   */
  private static class SomeOtherClass {
    /**
     * a demo method.
     */
    public void someMethod() {
      System.out.println("Protect me!");
    }
  }
}

/* ########## End of File ########## */
5.12.static Member
5.12.1.Static Members
5.12.2.Define the static member
5.12.3.Demonstrate static variables, methods, and blocks.
5.12.4.Inside main( ), the static method callme( ) and the static variable b are accessed outside of their class.
5.12.5.reference static field after declaration
5.12.6.static section of Initializer
5.12.7.static section Initializer and logic
5.12.8.Initializer for final static field
5.12.9.Demonstrates Static nested Classes
5.12.10.Reference inner static class
5.12.11.Static field, constructor and exception
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.