• Post Reply Bookmark Topic Watch Topic
  • New Topic

Isn't it convenient to use static variables and methods 'all the time'?  RSS feed

 
Matthew Park
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I just studied what 'static' keyword means in java - "Sharable"

I'm totally a beginner and have usually seen an error "non-static method *** cannot be referenced from a static context"

But since I started to use static keyword all the time - when defining variables and methods - I've never met any errors related to static keyword.

And here's my question, just like the title of this subject, isn't it convenient to use static variables and static methods all the time?

Why do Java need instance variables/methods?

I think it is more useful for coding without instance variables/methods.

I want to know when should I use static variables/methods and when should not and why should not use it in such cases.



Have a nice day!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew,
I can see why it would seem that way at first. Especially in small programs.

But then you get to larger programs and writing objects. In fact, you are probably using some right now. For example, suppose you want to create a List of Strings. If you only want one List, statics are fine. But what if you wanted two lists. You'd need to write something like:


If List didn't have instance variables, you couldn't have two lists. You'd be limited to one list for your whole program. As you write objects yourself, you'd run into the same problem.
 
Philip Thamaravelil
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The alternative is to use getters and setters. The value behind getters and setters is they hide the implementation details behind a given class.

For example, if you have a Person class to store some information. You could set static variables for firstName or lastName. But getters and setters allow us to apply behind the scenes business logic seamlessly to the calling class..

The getFirstName or getLastName methods could ... Check for null.. do Max length handling... etc..



Plus, you should read about thread synchronization in Java. Storing variables as static isn't thread safe.

Cheers,
Philip
 
Matthew Park
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Matthew,
I can see why it would seem that way at first. Especially in small programs.

But then you get to larger programs and writing objects. In fact, you are probably using some right now. For example, suppose you want to create a List of Strings. If you only want one List, statics are fine. But what if you wanted two lists. You'd need to write something like:


If List didn't have instance variables, you couldn't have two lists. You'd be limited to one list for your whole program. As you write objects yourself, you'd run into the same problem.


I didn't study GUI yet, but I guess your explanation works for me.
Maybe I should learn more... Thank you Jeanne!
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use only static variables then how do your instances differ from each other in terms of data..after all
isn't OOPS all about instances having individual identity.

Manish
 
Matthew Park
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philip Thamaravelil wrote:The alternative is to use getters and setters. The value behind getters and setters is they hide the implementation details behind a given class.

For example, if you have a Person class to store some information. You could set static variables for firstName or lastName. But getters and setters allow us to apply behind the scenes business logic seamlessly to the calling class..

The getFirstName or getLastName methods could ... Check for null.. do Max length handling... etc..



Plus, you should read about thread synchronization in Java. Storing variables as static isn't thread safe.

Cheers,
Philip

hmm...getters ansd setters......static isn't thread safe.........
Now I got it. Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!