aspose file tools*
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 Spring in Action this week in the Spring 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: 19720
    
  20

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: 24187
    
  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: 19720
    
  20

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: 39409
    
  28
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