my code ``` // Java class public class BaseOne { ...
# announcements
d
my code
Copy code
// Java class
public class BaseOne {
    public static void m1() {
        System.out.println("BaseOne");
    }
}

// redefined method in child _java_ class
public class BaseThree extends BaseOne {
    public static void m1() {
        System.out.println("BaseThree");
    }
}

// Kotlin 
class BaseTwo : BaseOne() {
        companion object { 
                fun m1() { // Error raises here :(
                        println("BaseTwo")
                }
        }
}
c
static methods cannot be overriden, that's one of their many drawbacks.
3
s
this ends up being less of a Kotlin question and more of a design pattern one
d
It means that there is no equal code in Kotlin?
maybe workarounds?
s
there are definitely ways to get around not being able to override static methods, but deciding which one to use depends on the context
d
Actually I need to convert to Kotlin this code: here is Grammar-kit method https://github.com/JetBrains/Grammar-Kit/blob/master/src/org/intellij/grammar/parser/GeneratedParserUtilBase.java#L871 it's easily redefined in custom java class https://github.com/kandeshvari/idea-nim/blob/master/src/org/dmitrigb/ideanim/parser/ParserUtil.java#L160 Is there a way to convert this code one-by-one without logic-rewrite?
m
@dimcha Not if you need to use it from java. If you only need to use the class from kotlin you can just not mark the method @JvmStatic and it should work.
r
That's not overriding in Java that's shadowing, hope this extra information helps to find a workaround
d
Unfortunately, these funcs from derived class are using in generated java code(Parser).
@robstoll thanks for correct definition, I hope it'll help.
p
this just confirms how much of a bad idea static methods are, I am glad Kotlin does not have them
k
@poohbar I don't agree, sometimes I really need functions that only makes sense in the context of a class and putting them in a companion object feels a bit verbose for an otherwise really compact language. Also having to put
@JVMStatic
everywhere...
c
If a function makes sense only in context of a class it should be either a top-level function or design is a bit awkward and needs to be re-done, at least that's the official position of Kotlin team. It makes sense to me, too.
d
@Czar ok, but what to do with alredy written java code? How to shadow exist static method in java code? As I understand it's not possible due to Kotlin limitations.