Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Methods

 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dmitry Mamonov
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1309
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the valuable inputs.

 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 50272
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Bartender
Posts: 1309
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1309
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did use terms functions and methods separately, Please correct me if wrong.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50272
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1309
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm, you are right
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic