Java Tutorial for Beginners - 21 - Variable Scope

preview_player
Показать описание
Variable Scope is discussed in this video. Scope? What? What on earth is that? Let's find out.

Рекомендации по теме
Комментарии
Автор

I've created a variation of both - local and global variables. Why getNumber method "can see" number2 variable, but does not print it?


public class Scope {

static int number2 = 14;

public static void main(String[] args) {


int number = 7;

System.out.println(number);


}


static void getNumber () {

System.out.println(number2);
}

}

kastisg
Автор

How about parameter scope? Should you differentiate that from local variables?

itsdannyftw
Автор

how would you create a method that contains a for loop with the starting(initialization) variable value passed by the parameter of the method as well as the end(terminating) variable. It might bet worded strangely so here is an example:

public static void main (String[ ] args) {

public static int addSum(int start, int end) {

for(start; end> start; start ++) {
sum = start + start;
return sum;
}
}
System.out.println ( addSum(1, 5) );
}

My ultimate goal is have to have two numbers, start (1) and end (5), I want to print out the sum of all numbers between 1 and 5. so 1, 2, 3, 4, 5 = 1 +2+3+4+5 = 15. But i want to be able to plug in 2 numbers into the parameter and have them passed into the for loop condition to become the initialization and termination. Is this possible? Please help or give any opinions. Any help or thoughts will help as i am stumped. Thank You.

DavidGarcia-uzox
Автор

is it compulsory to use static keyword while declaring global variables?

tejastoley
welcome to shbcf.ru