• Post Reply Bookmark Topic Watch Topic
  • New Topic

static  RSS feed

 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help: Anyone explain static word and why we use it . In simple words that i get the point .
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best explanation I've heard is that a static variable or method belongs to the class, not an instance of the class.

For beginners, using static variables is almost always a mistake.  There are very few reasons (some say none) for using a static variable.  A beginner may be tempted to use them because all of their code is in main() -- which is a mistake itself.

A static method needs no instance to call it, so you call it with the class name: MyClass.myMethod().  These can be used as utility methods.  Here's an example for illustrative purposes only:


You would call this like System.out.println(MyMath.sqaureMe(4.0));
 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know this part .But i wanna ask what is the use of static. Ever since I learn java first I watch a tutorial and don't know why I am doing it. What's the purpose of it, so I remember code and then after couple of weeks later I understand why and where to use it :P . That's tough though . Now same with static
 
Devaka Cooray
Marshal
Posts: 5650
765
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 'use' of the static keyword is you don't have to have an unnecessary object of the containing class just to call a method which is not dependent on objects of the containing class.

Consider Knute's example. If squareMe wasn't static, you will need an object of MyMath just to be able to call that method. That's an unnecessary thing since the logic in squareMe has nothing to do with the state of an object of MyMath.

Another use, when it comes to static variables, is that a static variable lets you share a value/object within objects.


 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static takes the component out of the realm of dynamic allocation for storage space. If you declare a variable as static, then literally only 1 storage area is allocated for that variable, no matter how many "instances" it is used in.

In the case of an instance variable:

Each time MyClass is instantiated you get storage allocated for myVariable. Each of these allocations are distinct, so you can do different calculations and not have any side effects from one instance to the other.

The case of the Class variable, static is used:

This changes the operation markedly: there is only one storage space for myVariable and that space is used by all instances of MyClass. This yields a very strange anomaly if you do not realize what static does--Each and every time myVariable is reference in any instance you are addressing the same storage space--the very same variable--so any manipulation instantly yields side effects throughout all instances of MyClass.

So if you want there to only be 1 copy of a certain Class, Method, or variable, then make it static, otherwise leave off the static and have the ability to have multiple instances that do not interact.

Saad Zahoor wrote:i know this part .But i wanna ask what is the use of static. Ever since I learn java first I watch a tutorial and don't know why I am doing it. What's the purpose of it, so I remember code and then after couple of weeks later I understand why and where to use it :P . That's tough though . Now same with static
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!