Programming (EN) 未分類

How to check errors when 'xxx' undeclared

I often get error and error often says 'xxx' undeclared. So I just organized my though what I should see when variable is undeclared.

1. Variable is not declared

If variable is not declared before use, it gets error.

Variable is declared as below.

//Declaring variable
Data_types Variable;

//example
int number1;
float number2;

2. Variable is not initialized

When variable is declared but not initialized, it gets error too. Not initializing means no initial variable is given to declared variable before use.

So initial number should be given as below.

//Declaring variable example
int number1;
float number2;

//Initializing variable example
number1 = 5;
number2 = 2.5;

//It's also okay to declare and initialize at the same time
int number 3 = 4;

3. Variable is used outside its scope

When declared and initialized variable is used outside its scope, it gets error too.

Thanks to this blog, I recognized that there are many factors that affects to scope errors in Java languages so I'll list these.

  • Local variables / Instance field / Static field.
  • If referred area is in instance method or static method.
  • Inheritance
  • Access modifier (public / protected / private)
  • Inner class
  • Lambda expression
  • Module

-Programming (EN), 未分類