class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
reference static field after declaration
public class ClassInitializer3 {
static int classField1 = 1;
static int classField2 = 1 + classField1;
public static void main(String[] args) {
System.out.println(classField1);
System.out.println(classField2);
}
}
static section of Initializer
public class ClassInitializer5 {
static String tz;
static {
java.util.Properties p = System.getProperties();
p.list(System.out);
tz = p.getProperty("user.timezone");
if (tz.equals(""))
tz = "Default";
}
public static void main(String[] args) {
System.out.println("timezone = " + tz);
}
}
static section Initializer and logic
public class ClassInitializer6 {
static int classField = 3;
static {
System.out.println(" : " + classField);
classField = 1;
for (int i = 2; i < 6; i++)
classField *= i;
}
static {
System.out.println(" = " + classField);
}
public static void main(String[] args) {
System.out.println(classField);
}
}
Initializer for final static field
public class ClassInitializer7
{
final static int classField;
static
{
classField = 29;
}
public static void main (String [] args)
{
System.out.println (classField);
}
}
Demonstrates Static nested Classes
/*
* 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:kraythe@arcor.de>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:kraythe@arcor.de>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:kraythe@arcor.de>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:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
* @version $Revision: 1.5 $
*/
protected static class YetAnotherClass {
}
/**
* A demo nested static class.
*
* @author <a href=mailto:kraythe@arcor.de>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 ########## */
Reference inner static class
class MyClassesDemo {
public static void main(String[] args) {
MyClass tlc = new MyClass();
MyClass.NestedMyClass ntlc;
ntlc = new MyClass.NestedMyClass();
}
}
class MyClass {
private int i;
private static String name = "MyClass";
{
System.out.println("Assigning 1 to i");
i = 1;
}
static class NestedMyClass {
int j;
{
System.out.println("Assigning 2 to j");
j = 2;
System.out.println(name);
}
}
}
Static field, constructor and exception
public class Main {
static Bar bar;
static {
try {
bar = new Bar();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] argv) {
System.out.println(bar);
}
}
class Bar {
public Bar() throws Exception {
}
public String toString() {
return "Bar";
}
}
http://www.java2s.com/Tutorial/Java/0100__
Class-Definition/Catalog0100__Class-Definition.htm
No comments:
Post a Comment