+ 4
What is difference between class varibales and local variables..??
3 Answers
+ 1
m looking for someone who can teach me Java script my WhatsApp number is +243840461536
0
A class variable is declared inside the class and it's visible to all methods and other variables as well (if It's public).
A local variable is declared inside a method (and consider that method can be outside the class you aim). This variable is only used by that method and only when the method is called
0
ex:
public static class Class1{
public static void print10( ){
private int value = 10;
System.out.println(value);
}
public class Class2{
public static void main(String [ ] args){
int otherValue = 20;
System.out.println(otherValue);
print10( );
}
}
in this example, you can't use directly or change the value of the variable "value" from the main class, because It's a local variable from the Class1, but you can use or change the value of the variable "otherValue" as well, because it's a class variable of the Class2 (the main class).