为什么以及何时将@JvmStatic与伴侣对象一起使用? [英] Why and when to use @JvmStatic with companion objects?

查看:919
本文介绍了为什么以及何时将@JvmStatic与伴侣对象一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解使用/不使用@JvmStatic与何时使用其中任何一个之间的区别。

I'm trying to understand the difference between using/not using @JvmStatic, and when I should use either one.

所以,使用Kotlin和Java,我可以这样做:

So, with Kotlin and Java, I can do this:

TestKotlin.kt

class TestKotlin {
    companion object {
        val someString = "hello world"
    }
}

然后由Java调用,如下所示:

Which is then called by Java, like this:

TestJava.java

public class TestJava {
    String kotlinStaticString = TestKotlin.Companion.getSomeString();
}

但是,有这个选项2:

TestKotlin.kt v2

class TestKotlin {
    companion object {
        @JvmStatic  // <-- notice the @JvmStatic annotation
        val someString = "hello world"
    }
}

然后,从Java调用它,如下所示:

And then, call it from Java, like this:

TestJava.java v2

public class TestJava {
    String kotlinStaticString = TestKotlin.getSomeString();
}

所以我的问题是:


  • 就行为或内存分配而言,这两种情况有什么不同吗?

  • 是否优先使用哪一种?

  • 两者都创建一个伪静态单例对象,就像Java静态一样吗?

谢谢!

推荐答案

@JmmStatic 注释的行为=http://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#static-methods\"rel =noreferrer>文档。在阅读文档时,您应该假设它为您提供了所有重要信息,并且文档中未提及的行为差异不存在。

The behavior of the @JvmStatic annotation is explained in detail in the documentation. When reading the documentation, you should assume that it gives you all the important information, and behavior differences that are not mentioned in the documentation do not exist.

在这种情况下,文档说如果你使用这个注释,编译器将在对象的封闭类中生成静态方法,在对象本身中生成实例方法。换句话说,注释的效果是它告诉编译器生成另一个方法。

In this case, the documentation says "If you use this annotation, the compiler will generate both a static method in the enclosing class of the object and an instance method in the object itself.". In other words, the effect of the annotation is that it tells the compiler to generate an additional method.

文档是否提到行为或内存分配有任何差异?它不是。因此,可以安全地假设没有。

Does the documentation mention that there is any difference in behavior or memory allocation? It does not. Therefore, it's safe to assume that there is none.

是否优先使用哪一个?通常,API在一个地方声明并在多个地方使用。如果你从Java调用一个方法,那么你应该将它声明为 @JvmStatic ,因为添加 @JvmStatic 在一个地方注释将允许您在多个地方省略多个 .Companion 引用。

Is there a preference on which one to use? Normally, an API is declared in one place and used from multiple places. If you're calling a method from Java, then you should declare it as @JvmStatic, because adding the @JvmStatic annotation in one place will allow you to leave out multiple .Companion references in multiple places.

两者都创建一个伪静态单例对象,就像Java静态一样吗?这个问题没有意义,因为Java static不会创建伪静态单例对象。如果在Java类中声明静态方法,然后调用此方法,则不会创建任何对象。

Do both create a pseudo static singleton object, like Java static does? This question does not make sense, because Java static does not create a "pseudo static singleton object". If you declare a static method in a Java class, and then call this method, no objects will be created.

这篇关于为什么以及何时将@JvmStatic与伴侣对象一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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