Instance and local variables in x++
Instance variables:
-> These variables are declared in the class declaration and can be accessed from any method in the class and subclasses that extend the class, if they are declared public or protected.
Local variables:
These variables are defined in a method and can only be accessed in that method.
The instance variables a and b can be accessed, and the parameters x and y act like local variables. Using parameters allows values to be assigned to those parameters when the method is called. If method1 is called by using the code
Test.method1(a,b);
, the x variable will be assigned the value Car and the y variable will be assigned the value 20. You can then use the values of the parameter variables to assign values to the instance variables c and d.Example:
class Test
{
str a = "Car"; // instance variable
int b = 20; // instance variable
str c; // instance variable
int d: // instance variable
void method1(str x, int y) // X and Y are local variables
{
c = x;
d = y;
}
}
Comments
Post a Comment