Topic: 求助:为何内部类不能有static变量

  Print this page

1.求助:为何内部类不能有static变量 Copy to clipboard
Posted by: needd
Posted on: 2005-09-17 12:33

/* class test{
  static int i=1;
  static void jb(){
    System.out.println("hao");
  }
}*/

public class statictestt{
  public static void main(String args[]){
    test.jb();
    }
  }
  
  class test{
  static int i=1;
  
  static void jb(){
    System.out.println("hao");
  }
  
}
}  

提示有错
书上说staic类不属于某个对象,但我不明白为何,内部类不能定义static 变量和方法?
各位大哥讲的时候最好说汉语,谢了,my english poor Smile

2.Re:求助:为何内部类不能有static变量 [Re: needd] Copy to clipboard
Posted by: why
Posted on: 2005-09-17 20:16

static inner class 可以有 static member 的,對嗎?Shy
(Anyway, a static inner class is not really an inner class.)

這是 Sun 的設計考慮吧 http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3
只能猜,而猜的又一言難盡
就算猜中了亦只是思維遊戲
思維遊戲自己做才有趣味,才有用
所以
如果閣下不想動腦筋(實在也未必值得),記住它就成了

3.Re:求助:为何内部类不能有static变量 [Re: needd] Copy to clipboard
Posted by: wek
Posted on: 2005-09-18 15:12

可以啊,谁说不可以呢。我改了改你的代码如下,希望这能帮到你。
public class statictestt
{
public static void main(String args[])
{
test.jb();
}
}

class test
{
static int i=1;

static void jb()
{
System.out.println("hao");
}

}

4.Re:求助:为何内部类不能有static变量 [Re: wek] Copy to clipboard
Posted by: why
Posted on: 2005-09-18 20:33

wek wrote:
可以啊,谁说不可以呢。我改了改你的代码如下,希望这能帮到你。

Have you tried to compile that piece of code? (class test has to be declared static)
I DON'T THINK SO. Sad

... oops, my mistake...
there's no inner class in your example.

5.Re:求助:为何内部类不能有static变量 [Re: why] Copy to clipboard
Posted by: wek
Posted on: 2005-09-19 08:20

why wrote:
Have you tried to compile that piece of code? (class test has to be declared static)
I DON'T THINK SO. Sad

... oops, my mistake...
there's no inner class in your example.

Oh,I am sorry that I make a mistake. I should have said that use the static inner class can do it. And here comes the example:

public class TestIner{
public static void main(String args[]){
test.jb();
}


static class test{
static int i=1;

static void jb(){
System.out.println("hao");
}

}
}

As you have said that static inner class is not a real inner class. Up to now,I don't know you are right or not.

6.Re:求助:为何内部类不能有static变量 [Re: wek] Copy to clipboard
Posted by: why
Posted on: 2005-09-19 09:44

wek wrote:
As you have said that static inner class is not a real inner class. Up to now,I don't know you are right or not.

JavaRanch Campfire - Getting in Touch with your Inner Class
from www.javaranch.com/campfire/StoryInner.jsp
I hate static!

You've probably heard about static inner classes. Well, they don't deserve to be called inner classes!

A static inner class (an inner class marked as static) looks like this:

class Outer
{
static class Inner
{
}
}

I don't like them because they don't give me that special object-to-object bond. In fact, static inner classes aren't even supposed to be called inner classes at all. Technically, they are "top-level nested classes".

A static nested class can be instantiated, but the object created doesn't share any special relationship with an outer object.

The static nested class is tied only to the outer class, not an instance of the outer class.

Outer.Inner i = new Outer.Inner();

That's why you can make an instance of the static nested class without having an instance of the outer class, just the way you can call static methods of a class without having any instances of that class. A top-level nested class is little more than another way to control namespace.



Anyway, I did use static inner class a few years ago... check this article on JavaWorld
http://www.javaworld.com/javaworld/javatips/jw-javatip106.html
or Item 18 of Effective Java Programming Language Guide

7.Re:求助:为何内部类不能有static变量 [Re: needd] Copy to clipboard
Posted by: smrar_car
Posted on: 2005-09-22 18:21

不能在内部类中声明静态变量是对java的内部类的一种保护,如果要想在内部类中声明静态变量必须把内部类声明为静态的!
如下:
public class StaticTest{
   public static void main(String args[]){
   //Test.jb();
   }
   }
  
class Test{
   // static int i=1;
  
  
   static class Inner
   {
     static final int j=0;
   }
}

8.Re:求助:为何内部类不能有static变量 [Re: needd] Copy to clipboard
Posted by: needd
Posted on: 2005-10-03 22:35

由于前一段时间有事没来,今天才来看我的问题,不过现在终于明白了,
感谢各位的帮忙,smrar_car ,why 还有wek (排名部分先后:))


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923