Unable to access member from inherited java class outside in kotlin
I am very new to Kotlin, I was trying to migrate my java project to kotlin and I ran into a problem where I can't access implicit inherited members from my class.
In java I had a class called Robot.java
which extends from TimedRobot.java
and inside the TimedRobot.java
there is a static member which is a double type
public class Robot extends TimedRobot {
}
TimedRobot.java
from the library:
public class TimedRobot extends IterativeRobotBase {
public static final double DEFAULT_PERIOD = 0.02;
private double m_period = DEFAULT_PERIOD;
...
}
In the same package as the Robot.java
I have a class called RobotConstants.java
which holds some numbers. Even though there was no variable called DEFAULT_PERIOD
in my Robot class I was still able to call it from outside like this:
public class RobotConstants {
public static final int PERIOD_IN_MS = (int)(Robot.DEFAULT_PERIOD * 1000);
...
}
However I am not able to do that in kotlin.
My robot class in kotlin:
class Robot : TimedRobot() {
}
And I am unable to get the default period in my constants object in kotlin, it gives me a compile time error. Here is what I am trying to do:
object RobotConstants {
const val PERIOD_Ms = (Robot.DEFAULT_PERIOD * 1000).toInt()
...
}
java inheritance kotlin
add a comment |
I am very new to Kotlin, I was trying to migrate my java project to kotlin and I ran into a problem where I can't access implicit inherited members from my class.
In java I had a class called Robot.java
which extends from TimedRobot.java
and inside the TimedRobot.java
there is a static member which is a double type
public class Robot extends TimedRobot {
}
TimedRobot.java
from the library:
public class TimedRobot extends IterativeRobotBase {
public static final double DEFAULT_PERIOD = 0.02;
private double m_period = DEFAULT_PERIOD;
...
}
In the same package as the Robot.java
I have a class called RobotConstants.java
which holds some numbers. Even though there was no variable called DEFAULT_PERIOD
in my Robot class I was still able to call it from outside like this:
public class RobotConstants {
public static final int PERIOD_IN_MS = (int)(Robot.DEFAULT_PERIOD * 1000);
...
}
However I am not able to do that in kotlin.
My robot class in kotlin:
class Robot : TimedRobot() {
}
And I am unable to get the default period in my constants object in kotlin, it gives me a compile time error. Here is what I am trying to do:
object RobotConstants {
const val PERIOD_Ms = (Robot.DEFAULT_PERIOD * 1000).toInt()
...
}
java inheritance kotlin
1
The static constants defined on a super type won't be accessible on the sub type in Kotlin (as opposed to Java). In your case, you could accessDEFAULT_PERIOD
only viaTimedRobot
– s1m0nw1
Jan 2 at 7:57
@s1m0nw1 thats a bummer to hear, but thank you for the clarification!
– Mahim Arib
Jan 2 at 8:04
add a comment |
I am very new to Kotlin, I was trying to migrate my java project to kotlin and I ran into a problem where I can't access implicit inherited members from my class.
In java I had a class called Robot.java
which extends from TimedRobot.java
and inside the TimedRobot.java
there is a static member which is a double type
public class Robot extends TimedRobot {
}
TimedRobot.java
from the library:
public class TimedRobot extends IterativeRobotBase {
public static final double DEFAULT_PERIOD = 0.02;
private double m_period = DEFAULT_PERIOD;
...
}
In the same package as the Robot.java
I have a class called RobotConstants.java
which holds some numbers. Even though there was no variable called DEFAULT_PERIOD
in my Robot class I was still able to call it from outside like this:
public class RobotConstants {
public static final int PERIOD_IN_MS = (int)(Robot.DEFAULT_PERIOD * 1000);
...
}
However I am not able to do that in kotlin.
My robot class in kotlin:
class Robot : TimedRobot() {
}
And I am unable to get the default period in my constants object in kotlin, it gives me a compile time error. Here is what I am trying to do:
object RobotConstants {
const val PERIOD_Ms = (Robot.DEFAULT_PERIOD * 1000).toInt()
...
}
java inheritance kotlin
I am very new to Kotlin, I was trying to migrate my java project to kotlin and I ran into a problem where I can't access implicit inherited members from my class.
In java I had a class called Robot.java
which extends from TimedRobot.java
and inside the TimedRobot.java
there is a static member which is a double type
public class Robot extends TimedRobot {
}
TimedRobot.java
from the library:
public class TimedRobot extends IterativeRobotBase {
public static final double DEFAULT_PERIOD = 0.02;
private double m_period = DEFAULT_PERIOD;
...
}
In the same package as the Robot.java
I have a class called RobotConstants.java
which holds some numbers. Even though there was no variable called DEFAULT_PERIOD
in my Robot class I was still able to call it from outside like this:
public class RobotConstants {
public static final int PERIOD_IN_MS = (int)(Robot.DEFAULT_PERIOD * 1000);
...
}
However I am not able to do that in kotlin.
My robot class in kotlin:
class Robot : TimedRobot() {
}
And I am unable to get the default period in my constants object in kotlin, it gives me a compile time error. Here is what I am trying to do:
object RobotConstants {
const val PERIOD_Ms = (Robot.DEFAULT_PERIOD * 1000).toInt()
...
}
java inheritance kotlin
java inheritance kotlin
asked Jan 2 at 7:48
Mahim AribMahim Arib
33
33
1
The static constants defined on a super type won't be accessible on the sub type in Kotlin (as opposed to Java). In your case, you could accessDEFAULT_PERIOD
only viaTimedRobot
– s1m0nw1
Jan 2 at 7:57
@s1m0nw1 thats a bummer to hear, but thank you for the clarification!
– Mahim Arib
Jan 2 at 8:04
add a comment |
1
The static constants defined on a super type won't be accessible on the sub type in Kotlin (as opposed to Java). In your case, you could accessDEFAULT_PERIOD
only viaTimedRobot
– s1m0nw1
Jan 2 at 7:57
@s1m0nw1 thats a bummer to hear, but thank you for the clarification!
– Mahim Arib
Jan 2 at 8:04
1
1
The static constants defined on a super type won't be accessible on the sub type in Kotlin (as opposed to Java). In your case, you could access
DEFAULT_PERIOD
only via TimedRobot
– s1m0nw1
Jan 2 at 7:57
The static constants defined on a super type won't be accessible on the sub type in Kotlin (as opposed to Java). In your case, you could access
DEFAULT_PERIOD
only via TimedRobot
– s1m0nw1
Jan 2 at 7:57
@s1m0nw1 thats a bummer to hear, but thank you for the clarification!
– Mahim Arib
Jan 2 at 8:04
@s1m0nw1 thats a bummer to hear, but thank you for the clarification!
– Mahim Arib
Jan 2 at 8:04
add a comment |
1 Answer
1
active
oldest
votes
You can either use the actual TimedRobot.DEFAULT_PERIOD
constant or redefine it via a companion object:
class Robot : TimedRobot() {
companion object {
const val DEFAULT_PERIOD = TimedRobot.DEFAULT_PERIOD
}
}
Another possibility is to represent these values as top level fields.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002900%2funable-to-access-member-from-inherited-java-class-outside-in-kotlin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can either use the actual TimedRobot.DEFAULT_PERIOD
constant or redefine it via a companion object:
class Robot : TimedRobot() {
companion object {
const val DEFAULT_PERIOD = TimedRobot.DEFAULT_PERIOD
}
}
Another possibility is to represent these values as top level fields.
add a comment |
You can either use the actual TimedRobot.DEFAULT_PERIOD
constant or redefine it via a companion object:
class Robot : TimedRobot() {
companion object {
const val DEFAULT_PERIOD = TimedRobot.DEFAULT_PERIOD
}
}
Another possibility is to represent these values as top level fields.
add a comment |
You can either use the actual TimedRobot.DEFAULT_PERIOD
constant or redefine it via a companion object:
class Robot : TimedRobot() {
companion object {
const val DEFAULT_PERIOD = TimedRobot.DEFAULT_PERIOD
}
}
Another possibility is to represent these values as top level fields.
You can either use the actual TimedRobot.DEFAULT_PERIOD
constant or redefine it via a companion object:
class Robot : TimedRobot() {
companion object {
const val DEFAULT_PERIOD = TimedRobot.DEFAULT_PERIOD
}
}
Another possibility is to represent these values as top level fields.
answered Jan 2 at 8:57
tynntynn
20.6k54883
20.6k54883
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002900%2funable-to-access-member-from-inherited-java-class-outside-in-kotlin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The static constants defined on a super type won't be accessible on the sub type in Kotlin (as opposed to Java). In your case, you could access
DEFAULT_PERIOD
only viaTimedRobot
– s1m0nw1
Jan 2 at 7:57
@s1m0nw1 thats a bummer to hear, but thank you for the clarification!
– Mahim Arib
Jan 2 at 8:04