java 静态和 IOException 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2601883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does static and IOException mean?
提问by Anshul Dawar
I just had a test on java and we had to give the definition of
1) Static:
2) IOExcepion:
我刚刚对 java 进行了测试,我们不得不给出
1) 静态的定义:
2) IOExcepion:
What I said for static was...a static method is used to define a method as a class method. And I got it wrong so I asked my teacher and he said he wants the actually definition of static not a static method, class or variable just static. Can someone tell me the definition of this and for IOException please Thanks.
我所说的静态是......静态方法用于将方法定义为类方法。我弄错了,所以我问我的老师,他说他想要静态的实际定义,而不是静态方法、类或变量,只是静态的。有人可以告诉我这个和 IOException 的定义吗谢谢。
回答by Justin Niessner
Static - A member/method that belongs to a class, not an instance of a class (Does not need to be instaciated to be used).
静态 - 属于类的成员/方法,而不是类的实例(不需要实例化即可使用)。
IOException - Exception thrown when there has been an Input/Output (usually when working with files) error.
IOException - 出现输入/输出(通常在处理文件时)错误时抛出的异常。
回答by Macarse
Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.
表示发生了某种 I/O 异常。此类是由失败或中断的 I/O 操作产生的异常的一般类。
A Java keyword used to define a variable as a class variable. Classes maintain one copy of class variables regardless of how many instances exist of that class. static can also be used to define a method as a class method. Class methods are invoked by the class instead of a specific instance, and can only operate on class variables.
用于将变量定义为类变量的 Java 关键字。无论该类存在多少个实例,类都维护类变量的一份副本。static 也可用于将方法定义为类方法。类方法由类而不是特定实例调用,并且只能对类变量进行操作。
回答by Justin Ardini
Staticmethods, fields, and nested classes are all associated with a Class, rather than an instance of that Class. From the Java tutorial:
静态方法、字段和嵌套类都与一个类相关联,而不是与该类的实例相关联。从 Java 教程:
the static keyword [is used] to create fields and methods that belong to the class, rather than to an instance of the class.
static 关键字 [用于] 创建属于类的字段和方法,而不是属于类的实例。
So, nested classes marked as static can be instantiated without an instance of the containing class. Because nested static classes are decoupled with the containing class, they do not have access to other members of the containing class.
因此,可以在没有包含类的实例的情况下实例化标记为静态的嵌套类。由于嵌套静态类与包含类分离,因此它们无法访问包含类的其他成员。
Similarly, static methods can be called by referring to ClassName.method(), and static variables by ClassName.variable.
同样,静态方法可以通过引用调用ClassName.method(),静态变量通过引用调用ClassName.variable。
IOException: A checked exception thrown when working with input/output operations (e.g. when dealing with files or sockets.)
IOException:在处理输入/输出操作(例如,处理文件或套接字时)时抛出的已检查异常。
回答by Hyman
the statickeyword is used on variablesor methods(even classes)to tell that they are associated with a classdefinition and not with an instance (of the class).
在static关键字上使用variables或methods(甚至classes)告诉他们正在与相关的class定义,而不是用(的类)的一个实例。
This means that every object of a class will share the same staticfields/methods because they are associated with the definition itself. Moreover you won't need any instance to refer to this field/methods because you can easily obtain them using the classitself.
这意味着一个类的每个对象都将共享相同的static字段/方法,因为它们与定义本身相关联。此外,您不需要任何实例来引用此字段/方法,因为您可以使用它们class本身轻松获取它们。
The concept of static classis used with nested class. Since a nested class may not be static, if you need to reference it from a static context you have to explicitly declare it to be static, otherwise the declaration would be associated with an instance, without allowing you to use it from static contexts..
的概念static class与嵌套类一起使用。由于嵌套类可能不是静态的,如果您需要从静态上下文中引用它,您必须明确声明它是静态的,否则该声明将与实例相关联,而不允许您从静态上下文中使用它。
the IOExceptionis just a subtype of Exceptionused by JDK to raise exceptions related to input/output problems. It's not a RuntimeException, so it's a checked one that must be caught whenever a method can throw it. This means that the exception is actually used to inform callers that something went wrong while working with I/O
这IOException只是ExceptionJDK 用于引发与输入/输出问题相关的异常的子类型。它不是RuntimeException,所以它是一个检查过的,只要方法可以抛出它,就必须捕获它。这意味着异常实际上是用来通知调用者在处理 I/O 时出现问题
回答by brydgesk
The above definitions for static are correct. As a quick example, think about if you had a "Bicycle" class. If you wanted a variable to store the number of wheels, it would make sense to be a static variable. If you created 100 Bicycle objects, there's no point in storing the number of wheels 100 times, since all Bicycles have two wheels.
上述静态定义是正确的。举个简单的例子,想想你是否有一个“自行车”课程。如果你想要一个变量来存储轮子的数量,那么作为一个静态变量是有意义的。如果您创建了 100 个 Bicycle 对象,则存储 100 次轮子的数量没有意义,因为所有 Bicycle 都有两个轮子。
回答by Anshul Dawar
1.Static-The static keyword in Java means that the variable/function is shared between all instances of that perticular class as it belongs to type, not the actual objects themselves.
1. Static- Java 中的 static 关键字意味着变量/函数在该特定类的所有实例之间共享,因为它属于类型,而不是实际对象本身。
2.IO Exception- Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. It is generally used in IO file.
2. IO Exception- 表示发生了某种 I/O 异常。此类是由失败或中断的 I/O 操作产生的异常的一般类。一般用在IO文件中。

