• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why static method can't?  RSS feed

 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I know that the static method can't access the non-static variables. I want the reason why the static method can't access the non-static variables?

Another thing, What is the use of Static methods? I know the importance of the static variable. These variables are one per class not per object.

I just want to know the use of static methods and place where it is frequently used.

Regards,
Niyas
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so you say you know what static methods are.
But to reitterate: static methods are methods belonging to the class itself, not to any specific instance of the class.

Now if a static method were to be able to access non-static datamembers (which by definition belong to a specific instance of the class), how would the JVM be able to tell which class instance were meant?

Typically (and correctly) static methods are NOT called on a class instance after all, but directly on the class itself.
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to have a grasp as to what static is for, so if you take the reasoning one step further you will find your answer. Since static means that it belongs to the class, and you don't need a specific instance, what would happen if you called a static method and no instance of the object exists?

This explains why the static method cannot access instance variables. The method is for the class as a whole, so it cannot access individual instances' fields (or methods).

As to why static methods exist at all - well, first of all, what if there were no static methods? What would happen when you tried to run your code? Since there is no such thing as a static method, you cannot call any methods on any classes for which there is no instance. Since all code must be inside a class, there would be no code at all which could execute. We get by this dilemma with static methods (usually main()).

Another use for a static method is to restrict access to constructors (i.e. Singleton pattern) in which a private constructor is declared and a static method is provided to return the instance. Without the static method, there would be no way of getting an instance of the class.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to continue, static methods are also sometimes used as 'helper' methods. think of the Math class. Do i really NEED an object created to let me calculate 18 raised to the 4th power? wouldn't it be nice if there was just a method i could call without having to create an instance of the Math class first?
 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your reply. For me it seems to be both understandable and not understandable. Could you pls provide some codings relate with your answer.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!