wood burning stoves*
The moose likes Java in General and the fly likes Static Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Static Methods" Watch "Static Methods" New topic
Author

Static Methods

Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Hi Ranchers,

I am aware of the Static keyword in java, its meaning and usage, but i have some confusion.

We generally use non-static method in java, but why not static methods

Please let me know the disadvantages of making all the methods 'static' in java.

Thanks,
Sumeet


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18763
    
  40

Please let me know the disadvantages of making all the methods 'static' in java.


Static methods doesn't have access to the "this" variable, which I guess is not a problem if you don't use objects very much. This is probably why new users to OOPs prefer static methods.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

Static methods don't need an instance, so they can be called without creating one. Utility methods and factory methods are good examples of method that should be static.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dmitry Mamonov
Greenhorn

Joined: Dec 24, 2008
Posts: 28
You can't override static method in subclass, this way static methods is more rigid than dynamic.
Even if method is instance-free (not using "this").
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Sut Pat wrote:We generally use non-static method in java, but why not static methods


Fill in the Blanks :



Sut Pat wrote:Please let me know the disadvantages of making all the methods 'static' in java.


I do not know why would you want to do so ?

We make classes with methods which make sense in some context
Making all methods static would mean you are using a structural approach towards programming [in an object oriented environment]

OOPs is all about objects [and other crap ]

by the way, it still makes sense to use static methods when for example you want something to be accessible without creating objects.



My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Thank you all for the valuable inputs.

Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296


Making all methods static would mean you are using a structural approach towards programming [in an object oriented environment]


Can you please explain this a bit?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38493
    
  23
I think by "structural", Sut Pat means "procedural". Only instance members (ie anything not marked static) can be used to represent or use the information in an object. Static members belong to the class, and all instances of that class have access to its static members, but imagine you have 3 instances of a class and a static method. Which instance would the static method manipulate if you call it? So static methods cannot access individual instances of the class.
Birla Murugesan
Ranch Hand

Joined: Nov 25, 2008
Posts: 66

see,if you use only static methods,then what is the use of instances of thier class .
what is the neccesity of object oriented programming if we go for only static metods.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Campbell Ritchie wrote:I think by "structural", Sut Pat means "procedural".


I think you mean "Salvin"

well i do not prefer the term procedural, cause even in procedural languages [like visual basic] we use classes and subroutines [and functions]

as opposed to structured languages [like C] where all functions are globally accessible and there is no concept of classes/methods


salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

I did use terms functions and methods separately, Please correct me if wrong.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38493
    
  23
Sorry about the wrong name.

I still suspect you have the wrong nomenclature. C is not a structured programming language, but procedural (although there are object-aware extensions of C, the best-known being called C++). I am not familiar with VB, but I think it is object-oriented, if you have classes.

In C they only ever say "function" and in Java we only ever say "method".
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

hmm, you are right
 
Don't get me started about those stupid light bulbs.
 
subject: Static Methods