This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Can't we declare a static variable inside a static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic
Author

Can't we declare a static variable inside a static method

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Consider this code:

When we compile

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.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Ramya,

What is the behavior of variables/object references when you declare them as static? What is the behavior of local variables/object references?

Now think, don't they both contradict?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

In C and C++ you can do this, but Java doesn't allow it. Instead, you should use a static class field.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
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.


Compile:

Regards.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by ramya narayanan:

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.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.

Try this:
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
That sounds correct.

Most fields will be instance fields (not labelled static), which means they can be different for each object/instance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't we declare a static variable inside a static method
 
Similar Threads
Assertions
assertion
declare static members inside inner class or method local inner class?
Regarding static
skipping synchronization on assuption of JAVA atomic primitive assignment.