Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I just have a (hopefully) simple question. How do I make a variable in one class that can be accessed by another class in Kotlin?
Class A:
var isBlue = 1
Class B:
if isBlue==1 then ...
.
someInstanceOfA.isBlue == 1
class A
class A { var isBlue = 1 }
class B
class B { var classA = A() fun demo(){ classA.isBlue//get A member } }
hope this helps.
Add a comment
you can either create an instance of the object and access the property like this
ClassA().isBlue
or inherit the class and access the attribute like this.
ClassB:ClassA{ fun someFn(){if (isBlue == 1) do something}}
In java , just declare with " static " , no need to call class name , in Kotlin need to call class name , but null pointer exception , Such headache problem
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
.dot notation to access its properties.someInstanceOfA.isBlue == 1.