• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static  RSS feed

 
Lionel Rossi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm newest of the Java programming, i don't use so much the word "static" in variable or methods because i didn't understand well how really do. Can anyone explain me in which situation i can use "static" or better when it recomand to use it?

I already do confusion with another word "final". Which are the difference between this two word?

Best Regards
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static member belongs to the class alone. This means that there is only one copy of the variable or method. So if you make three objects from the class, all the static members will be the same from object to object.

In general, a beginner should try to avoid static as much as possible. A good use of static would be if you have something that shouldn't change from object to object. One example might be a list of valid words for the class.

Using final when a variable is declared means the variable will not change value. A static variable can change value -- it's just that the value is the same for all objects. Using final and static when declaring a variable is used for constants, for instance, pi. Java has Math.PI declared. It is static, so you don't need an object to get the value. And it doesn't change, so it's final.

This is just a very basic summary. Others may fill it in.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . . Others may fill it in.
You have done it now. I shall have to inflict the most dubious classification of methods known to modern science on you. Note what Rob said, that methods inherited from interfaces and methods overridden should never be static.

Static methods in interfaces were introduced in Java8.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to what knute said
Saying that " if you declare a variable final then you cant modify it" will be somehow misleading

Using static with final then you cant modify it
Like:
Now you can't change x
But if you have

Now you can have different values of x for different objects
By passing different values in main
Like



Now you can pass different values




This creates one doubt in my mind

Why x don't take default value?
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe a final static variable can be initialized in a static block.

 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah!! But that didn't answer my question
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:. . .
Now you can have different values of x for different objects
That is the difference between an instance field and a static field.

. . .
Why x don't take default value?
It does. But the rules of Java® require it be initialised to a definite value (sometimes the same as the default value) before the class loading is completed (static final fields) or before the constructor completes (instance final fields). Try it without and you too can have a “might not have been initialised” error.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stores 0

While


Gives compilation error when we access x
 
Lionel Rossi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, thanks for your answers but i think that i need some more example to understand well this concept
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately we are not codemill
you should get your hands dirty,and start coding,and if you face any sort of problem, do post your specific queries.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:

Gives compilation error when we access x

The short answer of why is because it is defined that way. See section 16 of the JLS...

https://docs.oracle.com/javase/specs/jls/se7/html/jls-16.html

Local variables and final fields must be assigned before it can be used.


Any deeper answer to "why", you will likely need to talk to the Java designers.

Henry
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile that code. Because there is a constructor which doesn't give a specific value ti i, the compiler will complain bitterly. If you change the class, so there is no chance of i not being given a specific (=non‑default) value, it will compile. There are several things you can do:-
  • 1: delete the no-arguments constructor.
  • 2: Write i = 123; or similar in that constructor.
  • 3: Call the other constructor by writing this(123);
  • Remember that this(...); can only be written as the first line in a constructor.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!