• Post Reply Bookmark Topic Watch Topic
  • New Topic

global local variables?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I understand the concept of global and local variables but just one thing is confusing me,

how come you can't declare local variables in a method weather it be in main method or a static method or non static method as private,protected or public how come you can only declare global variables as this?


I thought you could declare a private variable in a method which would just be private to that method or class anyway iif you can how do you do so? and if you indeed can't which it looks like to me how come?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such thing as a global variable in Java. I assume you are meaning class or instance variables.

You don't specify access modifiers on method variables because they are not needed: all method variables are only accessible from within the method.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables declared in a method are by default private to that method and cannot be accessed from anywhere else. In contrast to variables declared on a class for which you have to define the access scope.

How would you even access someString inside the method? You can't, so there's no reason to assign an access scope to it. It's private, no matter what.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!