Reputation: 79
namespace hi
{
class hithere
{
public int numberOne = 12;
public int numberTwo = 12;
static void yo()
{
public int numberThree = 12;
private int numberFour = 12;
}
}
}
Can someone tell me the difference between the variables numberOne, numberTwo, numberThree and numberFour in this code excerpt?
Upvotes: 0
Views: 2062
Reputation: 26
numberOne and numberTwo are public instance variables in heap. They can be accessed directly inside of an object who has an instance of the hithere object. numberThree and numberFour cannot be accessed this way as they are not instance variables and are encapsulated within the scope of the function yo and stored in it respective stack.
Upvotes: 1
Reputation: 56984
numberOne and numberTwo are member variables of the class.
numberThree and numberFour are local variables, scoped to the function.
Next to that, you cannot declare an access modifier (private / public) for local variables.
Upvotes: 9