• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to understand static keyword.  RSS feed

 
Nikos Tsouktakos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, first time here!

Undergraduate student in a computing course in third year.

I recently got disappointed by the unfriendliness and hostility of stack overflow and thought I would try my luck here.

Keep in mind that English is not my native language.

Enough with the introduction, I am looking for an easy example to understand the use of the "static" keyword.
What the compiler thinks step by step in different cases is a huge help.

Thanks.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

We encourage folks around here to do some legwork first, tell us what they've done to try to solve their problem on their own, cite any relevant references, and then ask specific questions about what they don't understand.

The Java Tutorials on static keyword might be a good place to start to learn more so you can pose more specific questions here.

That said, it's not uncommon for Ranchers to go the extra mile and I wouldn't be surprised if someone posted a comprehensive writeup on the static keyword in response.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Yes, Stackoverflow is not always the best place for this type of question.

The static keyword basically means that the thing that is static (field, method, class) belongs to the class and not any instance of it. (Instances of classes are objects.)
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

As KS says, something static belongs to the class not the object. But it looks rather different for methods and fields.
I suggest you tell us what you think static fields mean and how they differ from instance fields. Leave the methods until later.
 
Nikos Tsouktakos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I understood a lot of things in the very first tutorial that you googled for me (feels ashamed!)

One question is, does a static method needs to interact with a static variable?
Also can a non-static method interact with a static variable?
 
Nikos Tsouktakos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome again

As KS says, something static belongs to the class not the object. But it looks rather different for methods and fields.
I suggest you tell us what you think static fields mean and how they differ from instance fields. Leave the methods until later.


A static variable is a variable that belongs to the class, and not to the instances of the class. It is useful to keep track of how many instanced I have created. I cannot think of more uses at the moment.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikos Tsouktakos wrote:
One question is, does a static method needs to interact with a static variable?
Also can a non-static method interact with a static variable?


One of the best ways to learn stuff is to just try it. So try it. Does it work? And if so, why do you think it works? Otherwise, why not?

So... Try it. Tell us why you think so. And we can give you hints in the right direction.

Henry
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikos Tsouktakos wrote:
Campbell Ritchie wrote:Welcome again

As KS says, something static belongs to the class not the object. But it looks rather different for methods and fields.
I suggest you tell us what you think static fields mean and how they differ from instance fields. Leave the methods until later.


A static variable is a variable that belongs to the class, and not to the instances of the class. It is useful to keep track of how many instanced I have created. I cannot think of more uses at the moment.


Oh, there are a lot of uses. Just one: what if you have a class where a method uses some constant value? There is no reason to have a copy of the same constant for every instance. In this case, it doesn't hurt if the field is an instance field, but static is better.
 
Nikos Tsouktakos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Nikos Tsouktakos wrote:
Campbell Ritchie wrote:Welcome again

As KS says, something static belongs to the class not the object. But it looks rather different for methods and fields.
I suggest you tell us what you think static fields mean and how they differ from instance fields. Leave the methods until later.


A static variable is a variable that belongs to the class, and not to the instances of the class. It is useful to keep track of how many instanced I have created. I cannot think of more uses at the moment.


Oh, there are a lot of uses. Just one: what if you have a class where a method uses some constant value? There is no reason to have a copy of the same constant for every instance. In this case, it doesn't hurt if the field is an instance field, but static is better.


So should class constants always be static or are there exceptions?
 
Nikos Tsouktakos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Nikos Tsouktakos wrote:
One question is, does a static method needs to interact with a static variable?
Also can a non-static method interact with a static variable?


One of the best ways to learn stuff is to just try it. So try it. Does it work? And if so, why do you think it works? Otherwise, why not?

So... Try it. Tell us why you think so. And we can give you hints in the right direction.

Henry


Hello, to answer my own question yes a static method needs to interact with a static variable.
Also a non-static method can also interact with a static variable.

I screenshoted the error message that I got when I tried to use a static method with a non-static variable:



 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikos Tsouktakos wrote: . . .
A static variable is a variable that belongs to the class, and not to the instances of the class. It is useful to keep track of how many instanced I have created. I cannot think of more uses at the moment.
There are many more uses.

Any field which you want to share between multiple instances might well be static.

If a constant is a constant it will always have the same value. It never changes. There is never any need for two copies of that value. So constants are usually marked static.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!