Click here to Skip to main content
14,696,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, Beginner asking:

1) What's the difference between declaring a variable outside of any {} beyond the uppermost ones?

and

2) declaring a variable within {}, but marking it as public?

For example:

class MyApplication // Variant 1
{
int variableInQuestion = 0;
}

versus

class MyApplication // Variant 2
{
public void MyMethod()
{
int variableInQuestion= 0;
}
}

From what I understand, not mentioning the access modifier means the variable gets the lowest possible scope. But I'm guessing this is overriden by the 'public' access modifier. Incorrect?

If the two examples are not equivalent, what is the difference between them?

Thanks

What I have tried:

I'm not sure how to write code to examine this, as I don't know what to look for. But I think I need to be able to understand/explain the difference.
Posted
Updated 17-Dec-17 9:35am
Comments
[no name] 17-Dec-17 13:39pm
   
In example 1, int variableInQuestion is a class member/variable.
In example 2, int variableInQuestion is a local variable of MyMethod and therefore visible only in MyMethod
Can't Code for Sure 17-Dec-17 13:42pm
   
Okay, thanks, so the access modifier does not make it visible beyond the local method.
[no name] 17-Dec-17 13:45pm
   
I don't get the Point what you mean. BTW I forgot to mention, in example 1, variableInQuestionis visible to all methods of class MyApplication

[Edit] I think now I got the Point.
The Access modifier in example 2 does only belong to the Method, but not to the local variables of the method itself.
Can't Code for Sure 17-Dec-17 13:48pm
   
Thanks again. I assumed that making it public had the same effect as stating it at the top.
[no name] 17-Dec-17 13:51pm
   
You are welcome. Wait some minutes, I'm sure some members here will give you a more complete answer... I hope ... :)
Can't Code for Sure 17-Dec-17 13:50pm
   
Dammit, I made an error :( I meant to say, for example 2:

class MyApplication // Variant 2
{
public MyMethod
{
public int VariableInQuestion = 0;
}
}
[no name] 17-Dec-17 13:52pm
   
That will not even compile I think :-)

[Edit]
BTW: In case you use "Reply" button local to a message, the message Sender will be informed.
Can't Code for Sure 17-Dec-17 13:54pm
   
That solves it. I am a moron.

Have a nice day.
[no name] 17-Dec-17 13:58pm
   
"...I am a moron", please not! Making mistakes is not bad and usually helps to learn :-)

What about 'The Documentation[^]'?
   
Comments
[no name] 17-Dec-17 14:10pm
   
Sometimes hard and usually boring :-)
See also comments to the question. Put this as an answer to remove it from unanswered Qs.

Example 1:
int variableInQuestion is a (private) class member/variable, visible to all Methods of class MyApplication

Example 2:
int variableInQuestion is a local variable of MyMethod and therefore visible only in MyMethod
   
Comments
Can't Code for Sure 17-Dec-17 14:43pm
   
Sorry, I misunderstood. There you see, I AM a moron after all. Better now?
[no name] 17-Dec-17 14:45pm
   
You are _not_ a moron! Same things happens to me when I first use this Forum. Yes better now and thank you very much for accepting.
Regards
Can't Code for Sure 17-Dec-17 14:46pm
   
Am grateful for the help
[no name] 17-Dec-17 15:56pm
   
And btw: "SorenKelstrup" was/is much more better than "Can't Code for Sure" ;)
Can't Code for Sure 17-Dec-17 16:00pm
   
Ouch, not my lucky day... Felt I needed to get an alias due to my fumblings above. Maybe one day I will laugh at this :)

I think it's really cool that one can get help so quickly.
[no name] 17-Dec-17 16:05pm
   
Again: Use the "Reply" button of the message in future and not the top most ... :laugh:

"I think it's really cool that one can get help so quickly." that depends from day to day and also of the mood of the volunteers actually online ;)
Can't Code for Sure 17-Dec-17 16:07pm
   
Still learning - and thanking. "Reply" is barely visible on my screen, did manage to find it now, though. Will let you return to more important matters.
[no name] 17-Dec-17 16:09pm
   
This time you got the right one ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900