C
C
Conan_Doyle2018-12-30 10:25:39
Java
Conan_Doyle, 2018-12-30 10:25:39

Which code is faster, where the variable is static or is it passed in the method?

There are two codes... which one is faster?

class Main{
    static MyClass myClass;
    public static void main(String[] args) {
        myClass = new MyClass();
        example();
    }
    static void example(){
        myClass.work();
        myClass.flex();
    }
}
//или же
class Main{
    public static void main(String[] args) {
        MyClass  myClass = new MyClass();
        example(myClass);
    }
    static void example(MyClass myClass){
        myClass.work();
        myClass.flex();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Therapyx, 2018-12-30
@Conan_Doyle

In theory, statics is faster. In practice, the difference is often not noticed, because even in more complicated cases, the compiler optimizes such cases.
But if you abuse static variables, then someday you will get slapped for it :D

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question