# Interface as variable?

**URL:** <https://discuss.kotlinlang.org/t/interface-as-variable/21690>\
**Category:** Support\
**Created:** [May 2, 2021, 5:02pm UTC](https://discuss.kotlinlang.org/t/interface-as-variable/21690 "2021-05-02T17:02:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlopez](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tlopez](https://discuss.kotlinlang.org/u/tlopez)\
**Post date:** [May 2, 2021, 5:02pm UTC](https://discuss.kotlinlang.org/t/interface-as-variable/21690/1 "2021-05-02T17:02:33Z")

</div>

Hi everyone, I am taking a mobile android development class and they are using Java but I was allowed to do it in Kotlin. In one of the examples, there is an interface being used in java as a variable.

I have been trying to get it to work in Kotlin, but it keeps crashing.

How would I be able to get it to function the same?

Java Code

```auto
public interface FragmentTracker {
 public void fragmentVisible(String s);
 public void goNext();
 public void goBack();
 public void saveNameAndLastName(String name, String lname);
 public void saveCityAndZip(String city, String zip);
 public void saveDetail(String detail);
 public void finished();
}

// class
public class Fragment1 extends Fragment {
 private FragmentTracker ft; // this is where it is being initialized as a variable
 private View v;
 public static final String fragmentTitle="Personal Info";
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) {
 //Toast.makeText(getContext(),"Visible",Toast.LENGTH_SHORT).show();
 ft.fragmentVisible(fragmentTitle);
 // Inflate the layout for this fragment
 v= inflater.inflate(R.layout.first_fragment, container, false);
 Button b_next=v.findViewById(R.id.next_button);
 b_next.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 ft.goNext();
 }
 });
 return v;
 }
```

This is what I tried in Kotlin

```auto
interface FragmentTracker {
    fun fragmentVisible(s: String)
    fun goNext();
    fun goBack();
    fun saveNameAndLastName(name: String, lname: String);
    fun saveCityAndZip(city: String, zip: String);
    fun saveDetail(detail: String);
    fun finished();
}

// the class
    private lateinit var ft: FragmentTracker // kotlin lets me do this but it fails below
    private var v: View? = null;

    companion object {
        const val fragmentTitle = "Personal Info";
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        ft.fragmentVisible(fragmentTitle) // it fails here
        v = inflater.inflate(R.layout.fragment_1, container, false)

        v?.findViewById<Button>(R.id.next_button)?.setOnClickListener {
            ft.goNext();
        }
        return v;
    }
```

Any help/guidance would be awesome

---

<div class="post-metadata">

**Author:** ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)\
**Post date:** [May 2, 2021, 6:38pm UTC](https://discuss.kotlinlang.org/t/interface-as-variable/21690/2 "2021-05-02T18:38:46Z")

</div>

Is the error “`lateinit property message has not been initialized`”?  
Runnable example:

```run-kotlin
fun main() {
    Foo().onCreate()
}

class Foo {
	lateinit var message: String // Non-nullable
	fun onCreate() {
	    println(message.length)
	}
}

```

If so, the error describes it well but I suspect explaining some of the terminology may help.Initialized means “set up”. To initialize a variable is to “give it a value”.

In you Java code you have a line as follows:

```java
private FragmentTracker ft; // this is where it is being initialized as a variable

```

^ Your comment is partially incorrect. Here you are declaring a variable–you are saying a thing by the name “ft” with the type of “FragmentTracker” is a variable belonging to the class. You are _not_ initializing the variable ft with a starting value. Technically Java adds a hidden initialization to `null` so that the real version in Java is: `private FragmentTracker ft = null;`

Or in fewer words:

```auto
val myVariable: String // Declaring a variable, saying it exists
myVariable = "Hello World" // Initiailzing a variable with a value, giving it a value

```

In Java, you can declare a variable without providing a starting value. Java gives your object a default value of `null` (or `0`, or `0.0`, or `false` depending on the type). Kotlin requires that you pick the starting value yourself.

If you were trying to make a one-for-one copy of your code in Kotlin, you could have declared your `ft` variable as nullable, `private var ft: FragmentTracker? = null`. Since in Java, all variables are nullable, this version is what your classmates are using when in Java.

It’s great to force null safety but sometimes we know a variable will never be null since the first thing someone does is call some starter function that sets the value. In this case, we no longer want to declare `ft` as nullable and deal with null safety all over the class. Instead, we want some convenient way of temporarily allowing it to be null, and we want to tell the compiler that we will take on the responsibility of making sure it is initialized properly through some other means.

Using `lateinit` allows us to say, this variable should never be null\*, however, we know it will be null for a brief moment until the class is initialized. Without `lateinit`, we’d only be able to perform this initialization in a constructor as constructors are always a valid place to give your class’s variables their starting values.

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [May 3, 2021, 4:12pm UTC](https://discuss.kotlinlang.org/t/interface-as-variable/21690/3 "2021-05-03T16:12:07Z")

</div>

> [@arocnies](#):
>
> you could have declared your `ft` variable as nullable, `private lateinit var ft: FragmentTracker?` .

I believe @arocnies meant it could be `private var ft: FragmentTracker? = null`.

---

<div class="post-metadata">

**Author:** ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)\
**Post date:** [May 3, 2021, 5:09pm UTC](https://discuss.kotlinlang.org/t/interface-as-variable/21690/4 "2021-05-03T17:09:16Z")

</div>

Good catch, I wrote that wrong by leaving out the null but I also accedintelly left in `lateinit`. What I meant was:  
The Kotlin equivalent to Java would be:

```kotlin
private var ft: FragmentTracker? = null

```

I edited my original reply

---

<div class="post-metadata">

**Author:** ![tlopez](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tlopez](https://discuss.kotlinlang.org/u/tlopez)\
**Post date:** [May 4, 2021, 4:22am UTC](https://discuss.kotlinlang.org/t/interface-as-variable/21690/5 "2021-05-04T04:22:59Z")

</div>

I figured out the issue. It was not the lateinit at all.

I was trying to do something with a toolbar when I needed to edit a TextView. I was able to resolve that

Thanks for the help and suggestions!
