This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Why can't we declare static property in a static method, when we can declare a instance property(non-static variable) in an instance method. Regards.
I think here's your problem right here. The variables you declare in an instance method are not instance variables -- they are local variables, whose scope is limited to the method that declares them. They are not attached to the object. Likewise, in a static method, the variables you declare are local variables, too.
Originally posted by ramya narayanan: So static means it belongs to the class and which cannot be a property of any methods not even static methods. Ex: now when I try to declare a static property in a static method, it shows error.
Because you are doing the same thing, only in a different method.
Joined: Oct 06, 2008
That is working & so static means it belongs to the whole class i.e anything primitive or class which belongs to the whole class can only be declared as static. Correct me If i'm wrong. Regards.