Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 590 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing Kotlin class object from Java

#1
I have a Kotlin class which has a class object, e.g.

public class Foo {
public class object {
public val SomeValue : Int = 0
}
}

If I'm using this class from Java, how do I access SomeValue inside the class object? If it were a Java class with a static property, I'd just use Foo.SomeValue - but I can't do that here.

IntellIJ shows that I can access `Foo.object.$instance`, but `$instance` doesn't have getSomeValue or anything like that. If I try to use $instance.SomeValue anyway, when I build the error message says:

> SomeValue has private access in Foo.object

I'm using Kotlin 0.5.1.
Reply

#2
The "absense" of getSomeValue() is a bug in the IDE. If you use it, it compiles OK.
I created an issue:

[To see links please register here]

Reply

#3
As a workaround, you should be able to make the Kotlin field visible using `@JvmField`:

@JvmField var addressLocationBox: ToOne? = null


Reply

#4
```
data class YourClass(@JvmField val name: String)
```
Reply

#5
To set a custom name for the generated Java class, use the @JvmName annotation:

**Kotlin**

@file:JvmName("DemoUtils")

package com.example

class Util

fun getTime() { /*...*/ }

**Java**

new com.example.Util();
com.example.DemoUtils.getTime();
Moreover, if you use the **@JvmMultifileClass**, then:

@file:JvmName("Utils")
@file:JvmMultifileClass

package com.example

fun getDate() { /*...*/ }

**Java**


com.example.Utils.getDate();
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through