`
jianchen
  • 浏览: 343162 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

java中static之对于class

阅读更多

以下摘自于javaworld的一个问题的解答:

写道
Q:Can a class (whether an inner or outer class) be declared static?

 

A:In order to understand the use of the static keyword in class declaration, we need to understand the class declaration itself. You can declare two kinds of classes: top-level classes and inner classes.

 

Top-level classes

You declare a top-level class at the top level as a member of a package. Each top-level class corresponds to its own java file sporting the same name as the class name.

A top-level class is by definition already top-level, so there is no point in declaring it static; it is an error to do so. The compiler will detect and report this error.

Inner classes

You define an inner class within a top-level class. Depending on how it is defined, an inner class can be one of the following four types:

1. Anonymous. Anonymous classes are declared and instantiated within the same statement. They do not have names, and they can be instantiated only once.

The following is an example of an anonymous class:

okButton.addActionListener( new ActionListener(){
   public void actionPerformed(ActionEvent e){
      dispose();
   }
});
 

Because an anonymous class doesn't have a normal class declaration where it's possible to use static , it cannot be declared static.

 

2. Local. Local classes are the same as local variables, in the sense that they're created and used inside a block. Once you declare a class within a block, it can be instantiated as many times as you wish within that block. Like local variables, local classes aren't allowed to be declared public, protected, private, or static.

Here's a code example:

//some code block .......{
   class ListListener implements ItemListener {
      List list;
      public ListListener(List l) {
         list = l;
      }
      public void itemStateChanged(ItemEvent e) {
         String s = l.getItemSelected();
         doSomething(s);
      }
   }
   List list1 = new List();
   list list2 = new List();
   list1.addItemListener(new ListListener(list1));
   list2.addItemListener(new ListListener(list2));
}
 

3. Member. Member classes are defined within the body of a class. You can use member classes anywhere within the body of the containing class. You declare member classes when you want to use variables and methods of the containing class without explicit delegation.

The member class is the only class that you can declare static. When you declare a member class, you can instantiate that member class only within the context of an object of the outer class in which this member class is declared. If you want to remove this restriction, you declare the member class a static class.

When you declare a member class with a static modifier, it becomes a nested top-level class and can be used as a normal top-level class as explained above.

 

4. Nested top-level. A nested top-level class is a member classes with a static modifier. A nested top-level class is just like any other top-level class except that it is declared within another class or interface. Nested top-level classes are typically used as a convenient way to group related classes without creating a new package.

 

If your main class has a few smaller helper classes that can be used outside the class and make sense only with your main class, it's a good idea to make them nested top-level classes. To use the nested top-level class, write: TopLevelClass.NestedClass .

 

See the following example:

public class Filter {
   Vector criteria = new Vector();
   public addCriterion(Criterion c) {
      criteria.addElement(c);
   }
   public boolean isTrue(Record rec) {
      for(Enumeration e=criteria.elements();
      e.hasMoreElements();) {
         if(! ((Criterion)e.nextElement()).isTrue(rec))
             return false;
      }
      return true;
   }
   public static class Criterion {
      String colName, colValue;
      public Criterion(Stirng name, String val) {
         colName = name; colValue = val;
      }
      public boolean isTrue(Record rec) {
         String data = rec.getData(colName);
         if(data.equals(colValue)) return true;
         return false;
      }
   }
}

 

And when you want to use it:

Filter f = new Filter();
f.addCriterion(new Filter.Criterion("SYMBOL", "SUNW"));
f.addCriterion(new Filter.Criterion("SIDE", "BUY"));
.....
if(f.isTrue(someRec)) //do some thing .....
 

One important note: The static keyword does not do to a class declaration what it does to a variable or a method declaration.

分享到:
评论

相关推荐

    java中static属性学习

    ### Java中的Static属性详解 #### 一、引言 在Java编程语言中,`static`关键字扮演着重要的角色。它允许我们创建与类关联而非特定实例关联的数据成员和方法。本文将详细介绍`static`属性的基本概念、静态成员变量...

    Java中的Static块与静态方法

    在Java编程语言中,`static`关键字扮演着至关重要的角色,它被用于声明类成员(如变量、方法)为静态的。静态成员不依赖于任何特定的对象实例,而是与类本身绑定,这意味着无论该类有多少个实例,静态成员只会有一份...

    java视频 static关键字

    在Java编程语言中,`static`关键字是一个非常重要的...理解和熟练使用`static`关键字对于提升Java编程技能至关重要。通过学习这个视频教程,你将能够深入理解`static`的含义和使用场景,从而更好地进行面向对象编程。

    java中staticclass静态类详解

    Java 中的静态类(Static Class)详解 Java 中的静态类是一种特殊的内部类,它可以被定义在外部类中,并且可以使用 static 关键字来修饰。静态内部类的使用可以带来很多便利,但是也存在一些限制。 静态内部类的...

    深入浅析Java中Static Class及静态内部类和非静态内部类的不同

    Java中的静态内部类(Static Class),也称为嵌套静态类,是Java语言提供的一种独特机制,它允许我们在一个外部类内部定义一个静态的类。这种类的声明前面带有`static`关键字,与普通的内部类(非静态内部类)有所...

    Java中static、this、super、final用法.doc

    在Java编程语言中,`static`、`this`、`super`和`final`是四个非常重要的关键字,它们各自有着特定的用途和含义。 1. **static**: 静态关键字`static`用于声明类级别的变量和方法,即静态变量和静态方法。静态变量...

    Java中的Static class详解及实例代码

    Java中的Static class详解及实例代码 Java中的Static class是指在Java中可以使用的静态实例变量、静态方法、静态块,还可以有静态类。静态类是指在一个类里面定义的静态内部类,也就是nested class。外部类中的静态...

    static 在java中的作用

    ### static在Java中的作用 `static`是Java语言中的一个关键字,主要用来修饰类成员(变量、方法或代码块),其核心功能在于定义类级别的共享属性。通过使用`static`,可以实现对类属性和方法的共享访问,而无需创建...

    java中的static变量,方法,代码块

    ### Java中的Static概念详解 在Java编程语言中,`static`关键字扮演着极其重要的角色,它主要用于声明类的成员变量、方法以及代码块为静态。本文将深入探讨`static`在变量、方法和代码块中的应用,通过示例和解释...

    java中static用法

    ### Java中static用法详解 #### 一、概述 在Java编程语言中,`static`关键字扮演着极其重要的角色。它被广泛应用于多种场景中,包括定义静态变量、静态方法、静态代码块以及静态内部类等。通过使用`static`,...

    Java类动态加载(一)——java源文件动态编译为class文件

    这篇博客“Java类动态加载(一)——java源文件动态编译为class文件”可能主要探讨了如何在运行时将Java源代码(.java)编译成对应的字节码文件(.class),并将其加载到Java虚拟机(JVM)中。以下是对这个主题的详细解析...

    Java static修饰方法

    在Java编程语言中,`static`关键字是一个非常重要的修饰符,它有多种用途,但最常见的是用来修饰类的方法和变量。在这个主题中,我们将深入探讨`static`修饰方法的概念、作用以及它如何影响Java程序的执行。 首先,...

    java中final和static的总结

    在Java编程语言中,`final` 和 `static` 是两个非常重要的关键字,它们分别具有不同的含义和用法,但都对代码的结构和行为产生深远影响。本文将深入探讨这两个关键字,并结合实际示例来解释它们的应用场景。 首先,...

    Java中static、this、super、final用法

    ### Java中static、this、super、final用法详解 #### 一、static关键字的使用 在Java编程语言中,`static`是一个非常重要的关键字,它主要用于定义类成员(包括变量和方法),使得这些成员能够在不创建类实例的...

    static在Java语言中的作用

    ### static在Java语言中的作用 #### 一、概述 `static`关键字在Java语言中具有重要作用,主要用于定义静态成员变量、静态方法、静态代码块以及静态内部类等。使用`static`关键字可以使某些属性和方法独立于类的...

    java中final与static的用法

    在Java编程语言中,`final`与`static`是两个非常关键且常用的关键字,它们各自具有独特的功能和用途,可以极大地增强代码的稳定性和可维护性。下面将详细解析这两个关键字的用法及其在Java中的重要性。 ### 一、`...

    Java中static、this、super、final用法简谈

    Java中的`static`、`this`、`super`和`final`是四个非常重要的关键字,它们在编程中扮演着至关重要的角色。这篇文章将简要介绍这些关键字的基本用法及其应用场景。 1. **static** `static`关键字用于声明类级别的...

    java中带static关键字的程序内存分析

    总的来说,`static`关键字在Java中有着广泛的应用,理解它的内存行为对于编写高效、无内存泄漏的代码至关重要。正确使用`static`可以优化内存管理,提高程序性能,而滥用则可能导致难以预料的问题。在实际编程中,应...

Global site tag (gtag.js) - Google Analytics