Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

👉 About the author

As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!

Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)

Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!

👉 premise

We learned Kotlin earlier, so let’s try Kotlin on Android.

If you are new, please finish learning Android basics first.

Recommended to see the small empty before writing stay up late Android series, and then try.

👉 Practice

A 😜 way

Java has the static keyword, and we often use it as an associated object in Kotlin, as follows:

class LoginFragment : Fragment(a){
    companion object {
        Public if there is no permission modifier by default
        const val APP_Name = "Mr. Empty Name"
        var APP_Name_Change = "Mr. Empty Name"
        private const val APP_Author = "Sesame seeds"}}Copy the code
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 private
        LoginFragment.APP_Name_Change="I changed your name."
        Log.e("TAG"."onCreate: " + LoginFragment.APP_Name_Change)
}
}
Copy the code

2021-10-19 16:04:26.574 22369-22369/cn. Appstudy E/TAG: onCreate: 2021-10-19 16:04:26.577 22369-22369/ cn.appStudy E/TAG: onCreate: I changed your name

That’s about using variables. What about methods? In Java, a method is static and a method is public. Kotlin?

companion object {
    fun myWork(a) {
        Log.e("TAG"."Method: My job is research and development")}Public if there is no permission modifier by default
    const val APP_Name = "Mr. Empty Name"
    var APP_Name_Change = "Mr. Empty Name"
    private const val APP_Author = "Sesame seeds"
}
Copy the code

As above in the normal functions, other KT files can be called.

Is it that simple?

Look, here’s the point. What if you call Kotlin from Java?

Let’s 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 be called
        // loginfragment.setapp_name_change (" Modify name "); // Cannot be called
        LoginFragment.Companion.myWork();
        LoginFragment.Companion.setAPP_Name_Change("Change the name"); }}Copy the code

As we know from practice, any declaration made in KT’s Companion Object cannot be called directly in Java. Instead, it is called with a Companion entity, which is equivalent to calling an instance method in a new class, rather than a static method.

So here’s what’s needed:

companion object {

        @JvmStatic
        fun myWork(a) {
            Log.e("TAG"."Method: My job is research and development")}Public if there is no permission modifier by default
        const val APP_Name = "Mr. Empty Name"
 
        @JvmStatic
        var APP_Name_Change = "Mr. Empty Name"
        private const val APP_Author = "Sesame seeds"
        // const const val is a const const const val. For static references in Java, @jvmField is required
        @JvmField
        val MY_APP = "My work."
}
Copy the code
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 be called
        // loginfragment.setapp_name_change (" Modify name "); // Cannot be called
        Log.e("TAG"."onCreate: " + LoginFragment.APP_Name);
        // These uses are normal, even if @jvmStatic is added, it can still be called as a Companion instance
        LoginFragment.Companion.myWork();
        LoginFragment.Companion.setAPP_Name_Change("Change the name");
        LoginFragment.myWork();
        LoginFragment.getAPP_Name_Change();
        Log.e("TAG"."onCreate: " + LoginFragment.MY_APP);
        Log.e("TAG"."onCreate: "+ LoginFragment.APP_Name); }}Copy the code

And then you run it and you try it again and you say, hey? Really, there’s not that much toast. That’s really good.

In order to implement the Java straight out of the static form

  • Add @jVMStatic for var
  • Const doesn’t matter
  • The @jvmStatic method is used
  • Nonconst qualifiers of type val use @jVMField

👉 other

📢 author: Kom and Kom in Kom

📢 reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)

📢 welcome to like 👍 collect 🌟 message 📝