0

If we define a variable as constant variable, when we use this variable in methods do we have to put method as static ?

static final int AGE=35;

private int daysOfLife(){
return AGE*365;
}

can we define method as like this ?

Even though it is not giving me any errors but is it a good practice to read static data from instance methods?

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
  • 2
    yes, you do need to put method as static. – Raju Sharma Sep 25 '17 at 05:30
  • 1
    Make the method static – Akshay Sep 25 '17 at 05:31
  • 6
    You can (I'd say you should) declare the method as static, but you don't have to. – Joe C Sep 25 '17 at 05:31
  • are you getting any errors? – Sabir Khan Sep 25 '17 at 05:32
  • There isn't showing any errors. But most of the people are saying you have to. Thats why I'm confused –  Sep 25 '17 at 05:37
  • Depends. If the method is not using any of the non-static members, then you can (and probably should) declare it as static too. – xiaofeng.li Sep 25 '17 at 05:40
  • @user2340012: Do they tell you the reason also for - **But most of the people are saying you have to**? I am just wondering why folks are suggesting that way if accessing static variables from instance methods is allowed and perfectly normal. My suggestion is - don't do it unless you understand the reasons. See [this](https://stackoverflow.com/questions/16880291/accessing-static-variables) too. – Sabir Khan Sep 25 '17 at 05:44
  • 2
    You absolutely don't have to. But why take our word for it when you can test it yourself? – shmosel Sep 25 '17 at 05:48
  • @user2340012: I have put a line in your question so readers have better clarity as what you are asking about. – Sabir Khan Sep 25 '17 at 05:52
  • thank you @SabirKhan. That's what I'm thinking. –  Sep 25 '17 at 05:54
  • 2
    *"is it a good practice to read static data from instance methods"* No. Yes. Whatever. There is nothing good/bad about use of static data in instance methods. Whether the method should be static or not has nothing to do with use of static data. There are a variety of reasons why a method cannot be static. If none of those are present, the method may be made static, and it is generally good to do so, but it is not required, and there might be reasons not to, so it is not a hard rule. – Andreas Sep 25 '17 at 06:06

3 Answers3

1

You shouldn't be only worried about variable / method being static or non - static but about other things too.

I would categorize your actions as - READ & WRITE and here you are trying to READ a default scoped , final & static variable in an INSTANCE , private method.

Concept of statics exits to logically group variables and methods so if your method has only that line and there isn't going to be anything else in that method, I would suggest to keep that grouping consistent and make either that variable an instance variable ( which doesn't make sense if variable is constant among all objects ) and change its scope to private ( if you don't wish variable to be available in same package classes ) OR mark that method as static.

Reading a final & static variable in an instance method is perfectly OK even though writing is questionable ( though final can't be written to but in case variable is not final ) .

Making that variable an instance one is favored if that variable is not going to be accessed by class name somewhere else and then if its going to be class level constant , make it static and change method to be static ( Initializing same constant field in every object will unnecessary cost you memory ) .

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
0

As much as i know ....

The 'static' means it is used in a class scope. Meaning it can be used in the entire program. So technically they can be stored in a non-static method, but they'll still be able to be used outside of that instance.

Zaki
  • 15
  • 5
0

1) there is no need to put method as static because static means it's for class when ever class start running static will run so static block is the thing will run first and only initialize once that's why it's not showing error in compile time

2) other way around we can't put initialize or use non static variable inside static block because static block will run before instance variable so compile time will catch the error

3) Variables that are declared final and are mutable can still be change in some ways; however, the variable can never point at a different object at any time .

4) so there nothing to worry about making method static

B_Ali_Code
  • 65
  • 10