为什么在Java方法中不允许静态变量,因为它们在C / C ++函数中? [英] Why are static variables not allowed inside Java methods as they are in C/C++ functions?

查看:136
本文介绍了为什么在Java方法中不允许静态变量,因为它们在C / C ++函数中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C C ++ 中,您可以:

void foo()
{
   static int i = 0;
   ++i;
}

但在 Java c $ c>'参数'错误的非法修饰符:

but, in Java you get a 'Illegal modifier for parameter' error for this:

public void foo()
{
    static int i =0;
   ++i;
}



我不是在寻找原因,为什么不是这是一个好/坏主意。我只是想知道为什么这在 C / C ++ 中是合法的,而不是 Java

I'm not looking for reasons as to why or why not this is a good/bad idea. I'm just curious why this is legal in C/C++, but not Java.

推荐答案

Java是一种功能差的语言。它不愿添加除了最简单的集合之外的功能,这将允许您编写好的程序。这意味着许多晦涩的功能,甚至一些非常有用的一些可用在其他语言在Java中不可用,因为通常有另一种方法来做同样的事情,或更好的方式,或者他们已经决定它是一个功能,你会更好的没有。

Java is a feature poor language. It is reluctant to add features other than the simplest set which will allow you to write good programs. This means many obscure features and even some very useful ones available in other languages are not available in Java because there is usually another way to do the same thing, or a better way or they have decided that it's a functionality you would be better off without.

在这种情况下,我怀疑这是因为你可以把字段放在方法之外,并做同样的事情。

In this case I suspect it is because you can just place the field outside the method and do much the same thing.

BTW:Java有方法局部命名的类,很少使用,Java并没有真正需要,如果他们是极简主义。 ;)

BTW: Java does have method local named classes, which are rarely used and Java didn't really need if they were being minimalist. ;)

这篇关于为什么在Java方法中不允许静态变量,因为它们在C / C ++函数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆