Win a copy of The Career Toolkit this week in the Jobs Discussion forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Static variables in Static method

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried the exumalator's question of the day, and I have a question:




1. char h = 1; //on the line before the System.out.println statement.
2. static char h = 1; //On the line before the System.out.println statement
3. char h = 1;//On the line before the main method
4. static char h = 1;//On the line before the main method


The answer is listed as 1 and 4, which is fine, but I also think 2 is also an option. Can't you put static variables inside a static method?
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by J Brewer:
Can't you put static variables inside a static method?



No. Only the final modifier is allowed.
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, static is a modifier you use on class variables and methods.
 
J Brewer
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's what I thought at first. But then I saw one source that said 'you can have static variables inside a static method', so I wasn't sure.

Thanks!
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Here I have one doubt that in general method we can put static variable but in static method why not.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only one modifier can be used by local variables that is final modifier.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i agree with Owen.....we cannot have any other variable modifier inside a method. As listed in the rules governing staic declarations in K & B book, I think that anything that is method-local cannot be static.
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic