简体   繁体   English

为什么我们不必在Java中创建System或Math类的对象并直接使用它们呢?

[英]Why don't we have to create object of System or Math classes in java and use them directly?

We use System.out.println without instantiating it or creating object of it. 我们使用System.out.println而不实例化它或创建它的对象。 Same goes to Math class and many others (I guess). 数学课和许多其他课程也是如此(我猜)。 Is there something special about these classes? 这些课程有什么特别之处吗? Can we use the classes and methods declared within those classes in same fashion? 我们可以以相同的方式使用在这些类中声明的类和方法吗? Please help. 请帮忙。

This is something called 'static' method. 这就是所谓的“静态”方法。 In order to invoke static method, you do not need to have an instance of the class. 为了调用静态方法,您不需要具有该类的实例。

This also has other side effects such as non-existing 'this' and thus static methods cannot invoke instance methods. 这还具有其他副作用,例如不存在“ this”,因此静态方法无法调用实例方法。

This is mostly used for some sort of utility classes which are often stateless. 这主要用于某些经常是无状态的实用程序类。

Math is a good example for it. 数学就是一个很好的例子。

I suggest to read a bit about static methods and static in Java in general. 我建议一般阅读一些有关静态方法和Java中的静态方法的知识。

You don't have to create objects for the System and Math classes because the methods and variables in those classes are static . 您不必为SystemMath类创建对象,因为这些类中的方法和变量是static This means that they belong to the class itself, not to instances of the class. 这意味着它们属于类本身,而不属于类的实例

For reference see: 供参考,请参阅:

Why don't we have to create object of System or Math classes in java and use them directly? 为什么我们不必在Java中创建System或Math类的对象并直接使用它们呢?

Because the methods of Math are declared as static methods, and because System.in / System.out / System.err are static variables. 因为Math的方法被声明为static方法,并且因为System.in / System.out / System.errstatic变量。

Is there something special about these classes? 这些课程有什么特别之处吗?

No. Any variables or methods that are declared as static will behave that way. 否。任何声明为static变量或方法都将以这种方式运行。

Can we use the classes and methods declared within those classes in same fashion? 我们可以以相同的方式使用在这些类中声明的类和方法吗?

I don't really understand what you are asking there. 我真的不明白你在问什么。 But, if you are asking if you can create an instance of Math or System so that you can do something like this: 但是,如果您询问是否可以创建MathSystem的实例,以便可以执行以下操作:

    Math myMath = new Math();
    myMath.min(1, 2);
  1. No, you can't. 不,你不能。 Neither of those classes has a public constructor, so you can't new them. 这些类都不具有公共构造函数,因此您不能new它们。

  2. And if you could do that, it would be really bad style! 而且,如果您能做到这一点,那将是非常糟糕的风格!

Reference: 参考:

We don't instantiate every other class or method because the JVM(Java Virtual Machine) already loads them into the project and hence, we can use these classes again and again. 我们不会实例化其他所有类或方法,因为JVM(Java虚拟机)已经将它们加载到项目中,因此,我们可以一次又一次地使用这些类。 One such example is the main method. 主要方法就是这样一个例子。 These classes/methods are already predefined for us so there is no need for us to instantiate such classes/methods because they are static. 这些类/方法已经为我们预先定义,因此我们无需实例化此类/方法,因为它们是静态的。

You don't need to create object of System and Math class to use it because they have static methods . 您无需创建SystemMath类的对象即可使用它,因为它们具有静态方法 Static methods belong to the class and thus doesn't require it to be instantiated. 静态方法属于该类,因此不需要实例化它。

Although, you can create its object and then also use those methods, but creating a class for static method is of no use. 虽然可以先创建其对象,然后再使用这些方法,但是为静态方法创建类是没有用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM