Java Programming - Declarations and Access Control - Discussion
Discussion Forum : Declarations and Access Control - Finding the output (Q.No. 5)
5.
What will be the output of the program?
public class Test
{
public int aMethod()
{
static int i = 0;
i++;
return i;
}
public static void main(String args[])
{
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
Answer: Option
Explanation:
Compilation failed because static was an illegal start of expression - method variables do not have a modifier (they are always considered local).
Discussion:
14 comments Page 1 of 2.
Dinu said:
1 decade ago
This not the correct explanation yaar
Kapil said:
1 decade ago
Static variables can only be declared as Global Variables (not local).
Manish said:
1 decade ago
We can only have final variable as local.
Raju said:
1 decade ago
The integer value returned is not obtained in any variable.
Is it not an error?
Is it not an error?
Snk said:
1 decade ago
We can only have final variable as local.
Manish said:
1 decade ago
We can not create object of static method.
Sonam said:
1 decade ago
Local variables can be final only.
No other modifier is allowed.Also it is not necessary that if a method is returning something we have to hold it.It is fine without that also.
No other modifier is allowed.Also it is not necessary that if a method is returning something we have to hold it.It is fine without that also.
Bhargavi said:
1 decade ago
Static variables cannot be instantiated and it could be accessed only by class-name.
Prabhakar said:
1 decade ago
We can't declare the static variable inside the non static method.
Because of (static variable are referring method area and instance variable referred heap area). And local variable must be final.
Because of (static variable are referring method area and instance variable referred heap area). And local variable must be final.
Vinodkumar said:
1 decade ago
When we use static it binds with class but there we are using static inside of non-static method so it won't bind with class because we got compilation error.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers