Staying up late and fighting Android - Kotlin - [what is Kotlin's static]

Keywords: Java Android kotlin

👉 About the author

As we all know, life is a long process, constantly overcoming difficulties and constantly reflecting on the process of progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share all my thoughts, experiences and stories in order to find resonance!!!
Focus on Android/Unity and various game development skills, as well as various resource sharing (website, tools, materials, source code, games, etc.)
If there is any need to welcome private self, exchange group, so that learning is no longer lonely.

👉 premise

Earlier, we learned the Kotlin language. Strike while the iron is hot. Let's try the application of Kotlin in Android.

If you are a novice, please complete the basics of Android first.

It is recommended to read the Android series written by Xiao Kong before staying up late and try again.

👉 Practice process

😜 Mode 1

The static keyword is commonly used in Java. It is an associated object in Kotlin. The usage is as follows:

class LoginFragment : Fragment() {
    companion object {
        //If there is no permission modifier by default, it is public type
        const val APP_Name = "Mr. empty name"
        var APP_Name_Change = "Mr. empty name"
        private const val APP_Author = "sesame seeds"
    }
}
class MainActivity : AppCompatActivity(), View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.e("TAG", "onCreate: " + LoginFragment.APP_Name)
        // Log. E ("tag", "oncreate:" + loginfragment. App_author) / / cannot be called because it is a private type
        LoginFragment.APP_Name_Change="I changed your name"
        Log.e("TAG", "onCreate: " + LoginFragment.APP_Name_Change)
}
}

Output results:
2021-10-19 16:04:26.574 22369-22369/cn.appstudy E/TAG: onCreate: Mr. Kong Ming
2021-10-19 16:04:26.577 22369-22369/cn.appstudy E/TAG: onCreate: I modified your name

The above is about the use of variables. What about the method? In Java, the method with the [static] keyword is static, and the public keyword is public. It can be used anywhere. Where's Kotlin?

companion object {
    fun myWork() {
        Log.e("TAG", "Method: my job is R & D")
    }
    //If there is no permission modifier by default, it is public type
    const val APP_Name = "Mr. empty name"
    var APP_Name_Change = "Mr. empty name"
    private const val APP_Author = "sesame seeds"
}

As above, some normal functions can be called in other kt files.

Is it that simple?

Attention, here's the point. What if we call Kotlin in Java?

We create a [TextActivity] and call:

public class TextActivity extends AppCompatActivity {
    public static String myName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);
        //LoginFragment.myWork();  // Cannot call
        //LoginFragment.setAPP_Name_Change("change name")// Cannot call
        LoginFragment.Companion.myWork();
        LoginFragment.Companion.setAPP_Name_Change("Modify name");
    }
}

From practice, we know that any declaration made in KT's company object cannot be called directly in Java, but by using the [Companion] entity, which is equivalent to calling instance methods instead of static methods for new classes.

Therefore, it is necessary to:

companion object {

        @JvmStatic
        fun myWork() {
            Log.e("TAG", "Method: my job is R & D")
        }

        //If there is no permission modifier by default, it is public type
        const val APP_Name = "Mr. empty name"
 
        @JvmStatic
        var APP_Name_Change = "Mr. empty name"
        private const val APP_Author = "sesame seeds"
        //The constant val of non const type is modified. To be used as a static reference in Java, @ JvmField is required
        @JvmField
        val MY_APP = "My work"
}
public class TextActivity extends AppCompatActivity {

    public static String myName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);
        //LoginFragment.myWork();  // Cannot call
        //LoginFragment.setAPP_Name_Change("change name")// Cannot call
        Log.e("TAG", "onCreate: " + LoginFragment.APP_Name);
        //These are used normally. Even if @ JvmStatic is added, it can still be called in the form of company instance
        LoginFragment.Companion.myWork();
        LoginFragment.Companion.setAPP_Name_Change("Modify name");
        LoginFragment.myWork();
        LoginFragment.getAPP_Name_Change();
        Log.e("TAG", "onCreate: " + LoginFragment.MY_APP);
        Log.e("TAG", "onCreate: " + LoginFragment.APP_Name);
    }
}

Try again after running, and you'll find, hey? Really, there isn't so much toast. It's really good.

To realize the static form directly pointed out in Java

  • Add @ JvmStatic for var type
  • const modified, don't worry
  • Method uses @ JvmStatic
  • Use @ JvmField if the non const modifier is of type val

👉 other

📢 Author: Xiaokong and Xiaokong in Xiaozhi
📢 Reprint instructions - be sure to indicate the source: https://zhima.blog.csdn.net/
📢 Welcome to praise 👍 Collection 🌟 Leaving a message. 📝

👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇

Posted by PCSpectra on Sat, 23 Oct 2021 18:28:01 -0700