• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C and C++ you can do this, but Java doesn't allow it. Instead, you should use a static class field.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds correct.

Most fields will be instance fields (not labelled static), which means they can be different for each object/instance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic