• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Why can't I make public variables in void methods?

 
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone. I am trying to use Slick2d to make a game, and I have a trouble initializing variables. I am using the BasicGame class to make my game. This class has three methods: init(), update(), and render(). init() is where I initialize variables. However, when I try to initialize a variable like this:

It doesn't work. However, when I put the SpriteSheet outside the init() method, it works. Why is that?
 
Sheriff
Posts: 27463
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variables which are declared inside methods (void methods or otherwise) are local to that method. In other words, they can't be accessed by any code outside the method.

That being the case, it doesn't make sense to declare them public, since the "public" modifier is specifically about how code outside the class can access the variable. It only makes sense to declare class-level variables to be public, as in your second example.
 
Alix Ollivier
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah. Thanks Paul.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok please also explain why we don't use private,protected,static and all other keyword except final keyword in local variable.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ritesh raushan wrote:ok please also explain why we don't use private,protected,static and all other keyword except final keyword in local variable.


Ritesh. Please don't ask your own questions in other people's threads. It's called "hijacking", and it's considered bad form (on almost any site).

If you have a question, create a thread of your own. Just look at the FAQ pages; it's very simple.

Thanks.

Winston
 
Marshal
Posts: 76885
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For exactly the same reason as you can’t say public.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Ritesh raushan wrote:ok please also explain why we don't use private,protected,static and all other keyword except final keyword in local variable.


Ritesh. Please don't ask your own questions in other people's threads. It's called "hijacking", and it's considered bad form (on almost any site).



Besides which the question was just answered right here.
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic