# Kotlin protected static function behaves differently as in Java

**URL:** https://discuss.kotlinlang.org/t/kotlin-protected-static-function-behaves-differently-as-in-java/7528
**Category:** Language Design
**Created:** [April 23, 2018, 5:28am UTC](https://discuss.kotlinlang.org/t/kotlin-protected-static-function-behaves-differently-as-in-java/7528 "2018-04-23T05:28:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Andras](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Andras](https://discuss.kotlinlang.org/u/Andras)
#### Post date: [April 23, 2018, 5:28am UTC](https://discuss.kotlinlang.org/t/kotlin-protected-static-function-behaves-differently-as-in-java/7528/1 "2018-04-23T05:28:43Z")

</div>

Sorry if this has already been posted.

Consider this Java code:

```
public class A {

    protected int number;

    public A(int number) {
        this.number = number;
    }
}

public class B extends A{

    private B(int number) {
        super(number);
    }

    public static B createFromA(A a) {
        return new B(a.number);
    }

}

```

Kotlin equivalent would be:

```
open class A(protected val number: Int)

class B private constructor(number: Int) : A(number) {

    companion object {
        @JvmStatic
        fun createFromA(a: A) = B(a.number)
        
    }
}

```

Though `a.number` is not visible from the static function.

Could anyone please help me how I could work around that fields with `protected` visibility are not visible from inheritors’ static function? Thank you in advance.

---

<div class="post-metadata">

### Author: ![forinil2](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/forinil2/32/2465_2.png) [@forinil2](https://discuss.kotlinlang.org/u/forinil2)
#### Post date: [April 23, 2018, 7:55am UTC](https://discuss.kotlinlang.org/t/kotlin-protected-static-function-behaves-differently-as-in-java/7528/2 "2018-04-23T07:55:35Z")

</div>

The only thing that I can think of is making public pseudo-getter:

```kotlin
open class A(protected val number: Int) {
	fun number() = number
}

class B private constructor(number: Int) : A(number) {

	companion object {
		@JvmStatic
		fun createFromA(a: A) = B(a.number())

	}
}

```

---

<div class="post-metadata">

### Author: ![Andras](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Andras](https://discuss.kotlinlang.org/u/Andras)
#### Post date: [April 23, 2018, 12:37pm UTC](https://discuss.kotlinlang.org/t/kotlin-protected-static-function-behaves-differently-as-in-java/7528/3 "2018-04-23T12:37:56Z")

</div>

I think I’ve found a slightly more elegant example than making it public. However, this seems very hacky and unintended by design.

```
open class A(protected val number: Int) {

    companion object {

        @JvmStatic
        protected val A._number
            get() = number
    }
}

class B private constructor(number: Int) : A(number) {

    companion object {
        @JvmStatic
        fun createFromA(a: A) = B(a._number)

    }
}

```

Could someone please shed some light on what the difference between Java-protected and Kotlin-protected visbility is?

---

<div class="post-metadata">

### Author: ![tieskedh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tieskedh/32/2816_2.png) [@tieskedh](https://discuss.kotlinlang.org/u/tieskedh)
#### Post date: [April 24, 2018, 8:12am UTC](https://discuss.kotlinlang.org/t/kotlin-protected-static-function-behaves-differently-as-in-java/7528/4 "2018-04-24T08:12:12Z")

</div>

Java protected means that it’s not only protected (that is children can access) but also package local.  
In kotlin, protected really means only children can access.
