Home > Enterprise >  I can't make variables work Android Studio
I can't make variables work Android Studio

Time:11-17

I have learned the java basics and am now trying to make an android app for my phone. I was doing fine up until I started using variables in android studio. Im fairly sure variables are meant to be declared like

var/val varName:Boolean false;

But whenever I do this I get an error saying "Cannot resolve symbol var".

I have researched but I cannot find any reason this is happening and no matter where I put this line of code it does not work. Everywhere I found online seems to say I'm doing it right but it doesn't work. I would love any adviceor how to make it work. Thanks

CodePudding user response:

I suspect you're confusing Java and Kotlin. Java variables are declared via this syntax:

type variableName = value;

In the example you've given, that would look like:

Boolean varName = false;

"var" and "val" are used in Kotlin. Android Studio supports both Java files (.java) and Kotlin files (.kt) in the same project. If you're just getting started and you're not sure which language to use, I recommend you consider using Kotlin instead of Java. Every software developer I know who knows Java and has tried Kotlin has ultimately concluded that they prefer working with Kotlin.

More info on Java variable declaration can be found here.

CodePudding user response:

This post is kinda confusing, in what language are you creating the app, java or kotlin? the syntax looks like Kotlin, but you are stating that you know Java basics (nothing said about kotlin)

Anyways, In kotlin, in order to create a variable you would do something like:

val a: Int = 1 // this is a VALUE, you cannot change the value of "val"
val b = 2  // this is also a value
var c = 2 // this is a variable, you can change the value of c
c = 5 // like I did here

var name: Boolean = false // and that's what I think you've tried to do
  • Related