Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Non-static variable cant accessed from the static context..your suggestion

807589Sep 13 2006 — edited Aug 18 2008
Once again stuck in my own thinking, As per my knowledge there is a general error in java.

i.e. 'Non-static variable cant accessed from static context....'

Now the thing is that, When we are declaring any variables(non-static) and trying to access it within the same method, Its working perfectly fine.
i.e.

public class trial{
���������� public static void main(String ar[]){ ////static context
������������ int counter=0; ///Non static variable
������������ for(;counter<10;) {
�������������� counter++;
�������������� System.out.println("Value of counter = " + counter) ; ///working fine
�������������� }
���������� }
}

Now the question is that if we are trying to declare a variable out-side the method (Non-static) , Then we defenately face the error' Non-static varialble can't accessed from the static context', BUT here within the static context we declared the non-static variable and accessed it perfectly.

Please give your valuable suggestions.

Thanks,
Jeff
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 15 2008
Added on Sep 13 2006
19 comments
362 views