免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5581 | 回复: 6
打印 上一主题 下一主题

新手请教个问题,关于static声明 [复制链接]

论坛徽章:
1
程序设计版块每日发帖之星
日期:2015-09-02 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-01-22 12:40 |只看该作者 |倒序浏览
本帖最后由 laolun 于 2014-01-22 12:41 编辑
  1. class BlockDemo
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.                 System.out.println("HAHAH");
  6.                 static int a=3;
  7.     }
  8. }
复制代码
代码如上,语句static int a=3;在执行的时候会报错,“错误: 非法的表达式开始”。研究了下static的用法,还是不明白为什么会出错。
特来请教各位,望指点迷津,谢谢!

论坛徽章:
0
2 [报告]
发表于 2014-01-22 16:02 |只看该作者
你如果用eclipse 这就不是问题,因为它的提示很清楚:
想在类里面用,必须写成
  final int a=3;
或者你把它放到main()上面

论坛徽章:
1
程序设计版块每日发帖之星
日期:2015-09-02 06:20:00
3 [报告]
发表于 2014-01-22 16:34 |只看该作者
回复 2# yansw
谢谢你的回答,目前用的是Editplus来写代码,所以还没有什么高级功能。
后来在网上找到这么一句话:静态变量是类变量,和方法是同一级别的,不能在方法中声明静态变量,main函数中同理。

目前还没找到权威资料来验证这个说法。


   

论坛徽章:
1
程序设计版块每日发帖之星
日期:2015-09-02 06:20:00
4 [报告]
发表于 2014-01-22 16:50 |只看该作者
本帖最后由 laolun 于 2014-01-22 17:02 编辑

另外附上我认为比较靠谱的其他解答:
解答1
Because the static variables are allocated memory at the class loading time,and the memory is allocated only once.Now if you have a static variable inside a method,then that variable comes under the method's scope,not class's scope,and JVM is unable to allocate memory to it,because a method is called by the help of class's object,and that is at runtime,not at class loading time.

解答2
Static variables belong to the class; variables declared inside a method are local variables and belong to that method. So you can call them final, but not static or public or protected or private.

论坛徽章:
31
CU大牛徽章
日期:2013-03-13 15:15:08CU大牛徽章
日期:2013-05-20 10:46:18CU大牛徽章
日期:2013-05-20 10:46:25CU大牛徽章
日期:2013-05-20 10:46:31CU大牛徽章
日期:2013-05-20 10:46:38CU大牛徽章
日期:2013-05-20 10:46:44CU大牛徽章
日期:2013-09-18 15:16:55CU大牛徽章
日期:2013-09-18 15:18:22CU大牛徽章
日期:2013-09-18 15:18:43CU十二周年纪念徽章
日期:2013-10-24 15:41:34丑牛
日期:2013-12-01 10:11:07水瓶座
日期:2014-01-15 08:47:25
5 [报告]
发表于 2014-01-24 12:36 |只看该作者
laolun 发表于 2014-01-22 16:50
另外附上我认为比较靠谱的其他解答:
解答1
Because the static variables are allocated memory at the  ...


这个解释非常详细。

那儿翻到的?

论坛徽章:
1
程序设计版块每日发帖之星
日期:2015-09-02 06:20:00
6 [报告]
发表于 2014-01-26 10:51 |只看该作者
回复 5# ddd010
从两个不同的帖子里找到的,其中一个是:
http://stackoverflow.com/questio ... ide-the-main-method



   

论坛徽章:
31
CU大牛徽章
日期:2013-03-13 15:15:08CU大牛徽章
日期:2013-05-20 10:46:18CU大牛徽章
日期:2013-05-20 10:46:25CU大牛徽章
日期:2013-05-20 10:46:31CU大牛徽章
日期:2013-05-20 10:46:38CU大牛徽章
日期:2013-05-20 10:46:44CU大牛徽章
日期:2013-09-18 15:16:55CU大牛徽章
日期:2013-09-18 15:18:22CU大牛徽章
日期:2013-09-18 15:18:43CU十二周年纪念徽章
日期:2013-10-24 15:41:34丑牛
日期:2013-12-01 10:11:07水瓶座
日期:2014-01-15 08:47:25
7 [报告]
发表于 2014-01-26 11:41 |只看该作者
laolun 发表于 2014-01-26 10:51
回复 5# ddd010
从两个不同的帖子里找到的,其中一个是:
http://stackoverflow.com/questions/3378758/ ...


谢谢。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP