Can you declare a static variable in a method?
Answers were Sorted based on User's Feedback
Answer / murali
No, Static variables can be declared as a class attributes,
for example,
public classA{
static String m2="xyz"; // this is correct form
public static void main(String args[]){
static String m1="abc"; // this gives an error
(illegal modifier for parameter;;....)
}
}
Is This Answer Correct ? | 25 Yes | 0 No |
Answer / vijayakumar chinnasamy
No static variable cannot be declared within static/non-
static method.
only "final" and "default" allowed within in method for
variable declaration. i.e u cant use
private,protected,public,static within method .it display
the compile time error "Illegal modifier".
Is This Answer Correct ? | 11 Yes | 1 No |
Answer / venkatesh
No we can't declare a static variable in a method.
only final and default scope variables are allowed inside
the method
Is This Answer Correct ? | 10 Yes | 0 No |
Answer / aravinda reddy
No we can't declare a static variable in a method or static
method, static variables are class level variables. Static
means one per class as per OO rules and method level
variables scope is with in the method. So if java allows
static variable inside a method, static concept is useless.
Is This Answer Correct ? | 8 Yes | 0 No |
Answer / ali
No Static Variable cannot declare in method.
but the method present in class can access the static variable
of class.
Or the method which is trying to access the static variable of
class but method is not part of that class at that time declare method as static then it can access that static variable.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / muthusenthil
No, you can declare static variable only in static method
Is This Answer Correct ? | 6 Yes | 19 No |
Difference between static binding and dynamic binding?
Does a class inherit the constructor of its super class?if it does, how can you hide that constructor? if it doesnot how can you call it from the sub class?
How many types of gc are there in java?
how to print hello world every second till i have pressed enter key ???
List out benefits of object oriented programming language?
What is the difference between error and an exception?
Can a class inherit the constructors of its superclass?
Difference between predicate, supplier and consumer ?
When do you create an index?
List some important features of java 10 release?
State the significance of public, private, protected class?
what modifiers are used with top-level class?